代码如下: 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...
* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private int maxDepth=0; public List<Integer> rightSideView(TreeNode root) { //require List<Integer> a...
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...
1/**2* Definition for a binary tree node.3* public class TreeNode {4* int val;5* TreeNode left;6* TreeNode right;7* TreeNode(int x) { val = x; }8* }9*/10publicclassSolution {11publicList<Integer>rightSideView(TreeNode root) {12List<Integer> result =newArrayList<Integer>();13...
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...
Leetcode solution 199:Binary Tree Right Side View Blogger:https://blog.baozitraining.org/2019/10/leetcode-solution-199-binary-tree-right.html Youtube:https://youtu.be/_g6pN64bF-o 博客园: https://www.cnblogs.com/baozitraining/p/11595617.html ...
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 <---
Given the following binary tree, 1 <--- / \ 2 3 <--- \ \ 5 4 <--- You should return[1, 3, 4]. Credits: 分析:层次遍历,取最右面的元素: 方法一:双q法: classSolution {public: vector<int> rightSideView(TreeNode *root)
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(); ...