当前位置:网站首页>Leetcode-199-right view of binary tree
Leetcode-199-right view of binary tree
2022-07-27 22:12:00 【benym】
# LeetCode-199- Right side view of binary tree
Given a binary tree , 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]
explain :
1 <---
/ \
2 3 <---
\ \
5 4 <---# Their thinking
Method 1、Queue iteration +BFS:
According to the idea of sequence traversal , Using a Queue To iterate , Add the right node first when traversing the sequence , Traverse the binary tree in the order of right and left roots
The node visible on the right is always the first pop-up node in the queue during sequence traversal , namely i==0 when , Add nodes to res in
Method 2、DFS:
We do a depth first search on the tree , During search , We always visit the right subtree first . So for each floor , The first node we see on this floor must be the rightmost node . thus , You only need to store the first node of each deep access
# Java Code 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
if(i==0){
res.add(queue.peek().val);
}
TreeNode temp = queue.poll();
if(temp.right!=null){
queue.add(temp.right);
}
if(temp.left!=null){
queue.add(temp.left);
}
}
}
return res;
}
}# Java Code 2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
if(root==null) return res;
DFS(root,0);
return res;
}
public void DFS(TreeNode root,int depth){
if(root==null) return;
// 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 .
if(depth>=res.size()){
res.add(root.val);
}
DFS(root.right,depth+1);
DFS(root.left,depth+1);
}
}边栏推荐
- 排序(冒泡排序)后面学习持续更新其它排序方法
- Log4j 漏洞仍普遍存在?
- 8000 word explanation of OBSA principle and application practice
- Matlab draws the statistical rose chart of wind speed and direction
- [stonedb fault diagnosis] system resource bottleneck diagnosis
- 九天后我们一起,聚焦音视频、探秘技术新发展
- Enumeration and annotation
- Matlab 绘制风速、风向统计玫瑰花图
- It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years
- Excalidraw:很好用的在线、免费「手绘」虚拟白板工具
猜你喜欢

光藕继电器

Pytoch distributed training

Finish learning redis cluster solution at one go

Matlab 绘制风速、风向统计玫瑰花图

matlab 绘制三坐标(轴)图

温度继电器

Can JVM tuning be done with single core CPU and 1G memory?

时间继电器

只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)

ThreadLocal principle and source code analysis (click in step by step, don't recite, learn ideas)
随机推荐
只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)
舌簧继电器
How to learn object Defineproperty | an article takes you to quickly learn
Finish learning redis cluster solution at one go
排序(冒泡排序)后面学习持续更新其它排序方法
Log4j 漏洞仍普遍存在,并持续造成影响
2022 2nd cyber edge cup cyber security competition Web
Leetcode 301. delete invalid parentheses
一种比读写锁更快的锁,还不赶紧认识一下
枚举和注解
每条你收藏的资讯背后,都离不开TA
After sorting (bubble sorting), learn to continuously update other sorting methods
Matlab 绘制风速、风向统计玫瑰花图
[numerical analysis exercise] Jacobi iteration method of third-order matrix
Shengyang technology officially launched the remote voiceprint health return visit service system!
Import word document pictures blocking and non blocking IO operations
对象在内存中存在形式&内存分配机制
ThreadLocal principle and source code analysis (click in step by step, don't recite, learn ideas)
Behind every piece of information you collect, you can't live without TA
The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"