当前位置:网站首页>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);
}
}边栏推荐
- Learn the use principle and core idea of thread pool from the source code
- day 1 - day 4
- Mysql 数据恢复流程 基于binlog redolog undolog
- C language output teaching calendar
- [numerical analysis exercise] numerical integration (complex trapezoid, complex Simpson, Romberg integral) C with STL implementation
- 学完4种 Redis 集群方案要多久?我一口气给你说完
- Station B collapsed. If we were the developer responsible for the repair that night
- @Can component be used on the same class as @bean?
- 一口气学完 Redis 集群方案
- Can JVM tuning be done with single core CPU and 1G memory?
猜你喜欢

It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years

In depth understanding of recursive method calls (including instance maze problem, tower of Hanoi, monkey eating peach, fiboracci, factorial))

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

matlab 绘制三坐标(轴)图

一口气学完 Redis 集群方案

Lvs+kept highly available cluster

零钱通项目(两个版本)含思路详解

Talk about MySQL transaction two-phase commit

MySQL execution process and order

IDEA常用快捷键及设置方法
随机推荐
8000字讲透OBSA原理与应用实践
【OBS】P B 丢帧阈值 buffer_duration_usec
Can JVM tuning be done with single core CPU and 1G memory?
2021-11-05 understanding of class variables and class methods
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool
2021-11-05理解main方法语法、代码块及final关键字
成员方法及其传参机制
Enumeration and annotation
More than 100 lines should be split into functions
关系型数据库的设计思想,20张图给你看的明明白白
MySQL series - database tables, queries, sorting, and data processing functions
Finish learning redis cluster solution at one go
固体继电器
Technology Management - we must focus on the big and let go of the small
Starrocks community structure comes out, waiting for you to upgrade!
V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
@Component可以和@Bean 用在同一个类上吗?
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
Memo mode - unity
Monitor the running of server jar and restart script