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...
问题链接 英文网站:199. Binary Tree Right Side View中文网站:199. 二叉树的右视图问题描述Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the n…
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...
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...
LeetCode199题 Binary Tree Right Side View, 解题思路。 1、先读题,凡是同一层的,有右边的节点,只添加右边的。 2、用bfs去解决,队列先添加右子树,再添加左子树,队列除了带node 信息,还得有当前层数的信息。 3、循环处理,当前层数没有被使用,就添加node的val。
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(); ...
【LeetCode】199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/binary-tree-right-side-view/description/ 题目描述: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered...
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...
class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> result = new ArrayList<>(); if (root == null) { return result; } LinkedList<TreeNode> queue = new LinkedList<>(); queue.addLast(root); int len = 1; ...
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 <---