当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢

Leetcode's 302 weekly rematch

手把手教你用 Power BI 实现 4 种可视化图表

突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战

setAttribute、getAttribute、removeAttribute

6-16 vulnerability exploitation -rlogin maximum permission login
向勒索病毒说不,是时候重塑数据保护策略

Custom scroll bar

How QT creator changes the default build directory

SSM在线租房售房平台多城市版本

基于Kubernetes v1.24.0的集群搭建(三)
随机推荐
Leecode-268. missing numbers (Application of XOR, find numbers that do not appear, find numbers that only appear once)
国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确
猿人学第五题
Getting started with SQL join use examples to learn left connection, inner connection and self connection
【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
基于Kubernetes v1.24.0的集群搭建(一)
有没有2、3w前期适合一个人干的创业项目呢?做自媒体可以吗?
1.4.1 many, many symbols (cin/cout unbinding, O3 optimization)
[function test] test of the project - login and post function
树莓派自建 NAS 云盘之——树莓派搭建网络存储盘
支持刘海屏
Why does 2.tostring() report an error
[leetcode]- linked list-3
Wechat applet learning five page Jump methods
使用Jenkins搭建CI服务器
Compatibility problems of call, apply, bind and bind
Ape anthropology topic 5
Buckle practice - maximum number of 28 splices
高速成长的背后,华为云乌兰察布数据中心的绿色之道
深圳地铁12号线第二批工程验收通过 预计7月28日试运行