当前位置:网站首页>29. Right view of binary tree
29. Right view of binary tree
2022-07-24 12:47:00 【Little happy】
199. Right side view of binary tree
Given a binary tree The root node root, Imagine yourself on the right side of it , In order from top to bottom , Returns the value of the node as seen from the right .
Example 1:

Input : [1,2,3,null,5,null,4]
Output : [1,3,4]
Example 2:
Input : [1,null,3]
Output : [1,3]
Example 3:
Input : []
Output : []
BFS Level traversal , Add the last data of each layer to the result
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Queue<TreeNode> q = new LinkedList<TreeNode>();
if(root==null) return res;
q.add(root);
while(!q.isEmpty()){
int n = q.size();
for(int i=0;i<n;i++){
TreeNode t = q.poll();
if(t.left!=null) q.add(t.left);
if(t.right!=null) q.add(t.right);
if(i==n-1) res.add(t.val);
}
}
return res;
}
}
DFS
Two 、DFS ( Time 100%)
Ideas : We are in accordance with the 「 Root node -> Right subtree -> The left subtree 」 Sequential access to , You can ensure that each layer is the first to access the rightmost node .
( And preorder traversal 「 Root node -> The left subtree -> Right subtree 」 Just the opposite , Go through each layer first, and the leftmost node is the first to be visited )
Time complexity : O(N), Each node accesses 1 Time .
Spatial complexity : O(N), Because this is not a balanced binary tree , The depth of a binary tree is at least logN, In the worst case, it will degenerate into a linked list , Depth is NN, Therefore, the stack space used in recursion is O(N) Of .
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
dfs(root, 0); // Access from the root , The root node depth is 0
return res;
}
private void dfs(TreeNode root, int depth) {
if(root==null){
return;
}
// First visit Current node , Then recursively access Right subtree and The left subtree .
if (depth == res.size()) {
// If the depth of the current node does not appear in res in , It indicates that the current node is the first accessed node at this depth , So add the current node to res in .
res.add(root.val);
}
depth++;
dfs(root.right, depth);
dfs(root.left, depth);
}
}
边栏推荐
- ERROR: [Synth 8-439] module ‘xxx‘ not found not found 错误解决办法
- 如何使用autofs挂载NFS共享
- 深圳地铁12号线第二批工程验收通过 预计7月28日试运行
- Get the current month and year and the previous 11 months
- Opencv:08 image pyramid
- 月薪 3万人民币是一种怎样的体验?做自媒体可以达到这种水平吗
- New applications of iSCSI and separation of storage services of NFS
- 向勒索病毒说不,是时候重塑数据保护策略
- 手把手教你用 Power BI 实现 4 种可视化图表
- 基于matlab的声音识别
猜你喜欢
![Error: [synth 8-439] module 'xxx' not found not found error solution](/img/47/bb03cc26e254332bf815c80bafb243.png)
Error: [synth 8-439] module 'xxx' not found not found error solution

Node takes effect after using NVM to install under Windows system, but NPM does not take effect

SSM在线校园相册管理平台

Implement is by yourself_ default_ constructible

How QT creator changes the default build directory

SQL JOIN 入门使用示例学习左连接、内连接、自连接

基于Qt的软件框架设计

【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12

高速成长的背后,华为云乌兰察布数据中心的绿色之道

Use typeface to set the text font of textview
随机推荐
Mobilevit: challenge the end-to-side overlord of mobilenet
leetcode第 302 场周赛复盘
for mysql
Is it safe to open an account on Oriental Fortune online? Is there a threshold for opening an account?
Wechat applet generates QR code
Node takes effect after using NVM to install under Windows system, but NPM does not take effect
Buckle practice - maximum number of 28 splices
Behind the rapid growth, Huawei cloud Wulanchabu data center is the green way
有没有2、3w前期适合一个人干的创业项目呢?做自媒体可以吗?
中国消费者和产业链都很难离开苹果,iPhone的影响力太大了
English语法_不定代词 - 概述
如何使用autofs挂载NFS共享
2022.07.15 暑假集训 个人排位赛(十)
Getting started with SQL join use examples to learn left connection, inner connection and self connection
I used annotations to configure the serialization of redis in one module project, and then introduced this module in another module. Why is this configuration
jsonp
STM32 - Fundamentals of C language
树莓派自建 NAS 云盘之——树莓派搭建网络存储盘
以Chef和Ansible为例快速入门服务器配置
iSCSI新应用,以及NFS的存储服务分离