当前位置:网站首页>Leetcode-199 - right view of binary tree
Leetcode-199 - right view of binary tree
2022-04-23 09:03:00 【z754916067】
subject
Ideas
- I've done a problem to distinguish each layer of binary tree traversal before , This problem should be based on that , Just add the rightmost node of each layer to the returned list . I wrote it directly AC. Look at the problem solution, which belongs to the solution of breadth first traversal , Then let's look at the solution of depth first traversal .
Code (BFS)
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new ArrayList<>();
// Create a queue
Queue<TreeNode> que = new ArrayDeque<>();
que.add(root);
while(!que.isEmpty()){
// The length of the current layer
int size = que.size();
// Each floor comes out together
for(int i=0;i<size;i++){
TreeNode temp = que.remove();
if(i== size-1){
// The last words Need to add ans
ans.add(temp.val);
}
// Enter it around the child
if(temp.left!=null) que.add(temp.left);
if(temp.right!=null) que.add(temp.right);
}
}
// return ans
return ans;
}
Code (DFS)
List<Integer> ans = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
// The root is traversed from right to left It can ensure that the node on the right is traversed first
// Need to maintain a depth To determine whether to join ans
DFS(root,0);
return ans;
}
public void DFS(TreeNode root,int depth){
if(root==null) return;
// If at present depth and ans equal Note No depth The nodes of the layer have not been added ans
// And as root right left traversal At this time root It must be the rightmost node and should be added
if(depth==ans.size()) ans.add(root.val);
// Continue traversing
DFS(root.right,depth+1);
DFS(root.left,depth+1);
}
版权声明
本文为[z754916067]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230722125313.html
边栏推荐
猜你喜欢
Redis Desktop Manager for Mac
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
MySQL小练习(仅适合初学者,非初学者勿进)
深度学习框架中的自动微分及高阶导数
Non duplicate data values of two MySQL query tables
Share the office and improve the settled experience
Idea import commons-logging-1.2 Jar package
LaTeX论文排版操作
I don't understand time, timestamp and time zone. Look at this article
Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
随机推荐
How does kubernetes use harbor to pull private images
Complete binary search tree (30 points)
After a circle, I sorted out this set of interview questions..
The K neighbors of each sample are obtained by packet switching
Consensus Token:web3. 0 super entrance of ecological flow
2021李宏毅机器学习之Adaptive Learning Rate
使用flask和h5搭建网站/应用的简要步骤
cadence的工艺角仿真、蒙特卡洛仿真、PSRR
Open services in the bottom bar of idea
Play with binary tree (25 points)
Project upload part
L2-3 浪漫侧影 (25 分)
MySQL查询两张表属性值非重复的数据
Go language self-study series | golang method
valgrind和kcachegrind使用運行分析
Idea import commons-logging-1.2 Jar package
npm ERR! network
单片机数码管秒表
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
Flink同时读取mysql与pgsql程序会卡住且没有日志