1privatevoiddfsHelper(Map<Integer, Integer> depthToValue, TreeNode node,intdepth) {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.va...
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...
代码如下: 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()...
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();13if(node->le...
1/**2* Definition for a binary tree node.3* struct TreeNode {4* int val;5* TreeNode *left;6* TreeNode *right;7* TreeNode(int x) : val(x), left(NULL), right(NULL) {}8* };9*/10classSolution {11public:12vector<int> rightSideView(TreeNode*root) {13intdep = -1;14bfs(roo...
classSolution{public:vector<int>rightSideView(TreeNode* root){ vector<int> res;if(!root)returnres; queue<TreeNode*> q; q.push(root);while(q.size()) {intsize = q.size();while(size--) {//先判断进入后马上-1autot = q.front(); ...
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. Example: Input: [1,2,3,null,5,null,4] Output: [1, 3, 4] Explanation: 1 <---
1/**2* Definition for a binary tree node.3* struct TreeNode {4* int val;5* TreeNode *left;6* TreeNode *right;7* TreeNode(int x) : val(x), left(NULL), right(NULL) {}8* };9*/10classSolution {11public:12vector<int> rightSideView(TreeNode*root) {13vector<int>res;14queue<...
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };*/classSolution {public: vector<int> rightSideView(TreeNode *root) { vector<int>ret; queue<TreeNode*>myque;if(NULL == root)returnret; myque.push(root);intlen =0; ...
链接:http://leetcode.com/problems/binary-tree-right-side-view/ 题解: 求二叉树right side view。 我们可以使用层序遍历,每次当前level == 0时,这个节点为此层最右边的节点,这时候我们把这个节点的value加入到结果集去。应该还有很多更好的方法,二刷要再细致一些。