当前位置:网站首页>LeetCode-199-二叉树的右视图
LeetCode-199-二叉树的右视图
2022-04-23 07:22:00 【z754916067】
题目

思路
- 之前做过一道分清楚二叉树遍历的每层的题,这道题应该在那个的基础上,把每层最右的节点加入返回的列表就好。写了一下直接AC。看了一下题解属于广度优先遍历的解法,那么再看一下深度优先遍历的解法。
代码(BFS)
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new ArrayList<>();
//创建队列
Queue<TreeNode> que = new ArrayDeque<>();
que.add(root);
while(!que.isEmpty()){
//当前层的长度
int size = que.size();
//每层一起出
for(int i=0;i<size;i++){
TreeNode temp = que.remove();
if(i== size-1){
//最后一个的话 需要加进ans
ans.add(temp.val);
}
//入它左右孩子
if(temp.left!=null) que.add(temp.left);
if(temp.right!=null) que.add(temp.right);
}
}
//返回ans
return ans;
}
代码(DFS)
List<Integer> ans = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
//根右左进行遍历 可以保证最先遍历到右边的节点
//需要维护一个depth用以判断是否加入ans
DFS(root,0);
return ans;
}
public void DFS(TreeNode root,int depth){
if(root==null) return;
//如果当前depth和ans相等 说明第depth层的节点还没有加入ans
//而作为根右左的遍历 此时的root一定是最右边且应该加入的节点
if(depth==ans.size()) ans.add(root.val);
//继续遍历
DFS(root.right,depth+1);
DFS(root.left,depth+1);
}
版权声明
本文为[z754916067]所创,转载请带上原文链接,感谢
https://blog.csdn.net/z754916067/article/details/124341789
边栏推荐
- Vowel substring in statistical string of leetcode simple problem
- Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi
- [Effective Go 中文翻译]函数篇
- 通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
- There are some problems when using numeric type to query string type fields in MySQL
- [appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
- Briefly describe the hierarchical strategy of memory
- NFT ecological development of Ignis public chain: unicorn Donation and development of Art
- colorui 解决底部导航遮挡内容问题
- 输入/输出系统
猜你喜欢

在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)

LeetCode簡單題之計算字符串的數字和

每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9

为什么会存在1px问题?怎么解决?

Campus transfer second-hand market source code download

Discussion on ES6 tail tune optimization

nn.Module类的讲解

ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建

怎么读书读论文

vslam PPT
随机推荐
LeetCode简单题之重新排列日志文件
Online yaml to XML tool
AQS & ReentrantLock 实现原理
Generate and parse tokens using JWT
基于TCP/IP协议的网络通信实例——文件传输
浅谈ES6尾调优化
Usage of databinding
怎么读书读论文
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
Rotation function of leetcode medium problem
Jetson Xavier NX (3) bazel mediapipe installation
Situational leaders - Chapter 7, solving performance problems
Comparison of indoor positioning technology
Common regular expressions
校园转转二手市场源码下载
redis主从服务器问题
Canvas learning Chapter 1
C language learning record -- use and analysis of string function (2)
Positioning of high precision welding manipulator
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)