1、题目描述 2、问题分析 使用层序遍历 3、代码 1vector<int>v;2vector<int> rightSideView(TreeNode*root) {3if(root ==NULL)4returnv;56queue<TreeNode*>q;7q.push(root);89while(!q.empty()) {10intsize =q.size();11for(inti =0; i < size; i++) {12TreeNode *node =q.front();13...
* TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };*/classSolution {public: vector<int> rightSideView(TreeNode *root) { vector<int>res;if(!root)returnres; queue<TreeNode*>q; q.push(root);while(!q.empty()) { res.push_back(...
1privatevoiddfsHelper(Map<Integer,Integer>depthToValue,TreeNode node,int depth){2if(node==null){3return;4}56// this is a pre-order traversal, essentially keep overwriting the depthToValue map (right view)7// while traverse the tree from left to right8depthToValue.put(depth,node.val);9...
https://leetcode.com/problems/binary-tree-right-side-view/ 题目描述 Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Example: ...
LeetCode 每日一题 Daily Challenge 199 Binary Tree Right Side View 84 -- 5:48 App LeetCode 每日一题 Daily Challenge 1032 Stream of Characters 90 -- 1:45 App LeetCode 每日一题 Daily Challenge 100 Same Tree 250 -- 5:57 App LeetCode 每日一题 Daily Challenge 117 Populating Next Right ...
199. Binary Tree Right Side View 题目 Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Example: AI检测代码解析 Input:[1,2,3,null,5,null,4]Output:[1,3,4]Explanation:1<---/\23<---\ ...
http://www.programcreek.com/2014/04/leetcode-binary-tree-right-side-view-java/ 看了这篇博客就懂了。 ** 总结: queue来遍历tree ** Anyway, Good luck, Richardo! My code: /** * Definition for a binary tree node. * public class TreeNode { ...
Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom. 一颗二叉树,获取从右边看去能看到的每层的第一个节点,就是每层最右侧的节点。 广度优先遍历,做一点小修改就行: ...
代码如下: publicList<Integer>rightSideView(TreeNoderoot){LinkedList<Integer>result=newLinkedList<>();if(Objects.isNull(root)){returnresult;}LinkedList<TreeNode>levelA=newLinkedList<>(),levelB=newLinkedList<>();levelA.addFirst(root);while(!levelA.isEmpty()){TreeNodetreeNode=levelA.removeLast()...
LeetCode199题 Binary Tree Right Side View, 解题思路。 1、先读题,凡是同一层的,有右边的节点,只添加右边的。 2、用bfs去解决,队列先添加右子树,再添加左子树,队列除了带node 信息,还得有当前层数的信息。 3、循环处理,当前层数没有被使用,就添加node的val。