当前位置:网站首页>[LeetCode]513. Find the value in the lower left corner of the tree
[LeetCode]513. Find the value in the lower left corner of the tree
2022-06-27 21:50:00 【A Fei algorithm】
513. Find the value in the lower left corner of the tree
Given a binary tree The root node root, Please find the of the binary tree At the bottom Leftmost The value of the node .
Suppose there is at least one node in the binary tree .
Example 1:
Input : root = [2,1,3]
Output : 1
Example 2:
Input : [1,2,3,4,null,5,6,null,null,7]
Output : 7
Tips :
The range of the number of nodes in a binary tree is [1,104]
-231 <= Node.val <= 231 - 1
Method 1:BFS
- Sequence traversal , Iterate each layer , Take the leftmost
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
TreeNode res = root;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
if (i == 0) res = q.peek();
TreeNode cur = q.poll();
if (cur.left != null) q.offer(cur.left);
if (cur.right != null) q.offer(cur.right);
}
}
return res.val;
}
Method 2:DFS
- First the left subtree node and then the right subtree node , When the left subtree switches to the right subtree, the maximum depth
maxDepth
Update and record of , And save the result value
public int findBottomLeftValue(TreeNode root) {
dfs(root, 0);
return res;
}
int maxDepth = -1, res = 0;
private void dfs(TreeNode root, int depth) {
if (root == null) return;
dfs(root.left, depth + 1);
if (depth > maxDepth) {
maxDepth = depth;
res = root.val;
}
dfs(root.right, depth + 1);
}
Follow Up: Find the value in the lower right corner of the tree
Method 1:BFS
- Record the last value of each layer
public int findBottomRightValue(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
TreeNode res = root;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
if (i == size - 1) res = q.peek();
TreeNode cur = q.poll();
if (cur.left != null) q.offer(cur.left);
if (cur.right != null) q.offer(cur.right);
}
}
return res.val;
}
Method 2:DFS
- Right subtree first and then left subtree
public int findBottomRightValue(TreeNode root) {
dfs(root, 0);
return res;
}
int maxDepth = -1, res = 0;
private void dfs(TreeNode root, int depth) {
if (root == null) return;
dfs(root.right, depth + 1);
if (depth > maxDepth) {
maxDepth = depth;
// System.out.printf("%d->",root.val);
res = root.val;
}
dfs(root.left, depth + 1);
}
边栏推荐
猜你喜欢
Tiktok's interest in e-commerce has hit the traffic ceiling?
集合代码练习
AI painting minimalist tutorial
JVM memory structure when creating objects
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
∫(0→1) ln(1+x) / (x ² + 1) dx
Codeforces Global Round 14
Go from introduction to practice -- coordination mechanism (note)
微服务之远程调用
GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
随机推荐
GBase 8a OLAP分析函数 cume_dist的使用样例
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
Go from starting to Real - Interface (note)
[LeetCode]515. 在每个树行中找最大值
[leetcode] 508. Élément de sous - arbre le plus fréquent et
∫(0→1) ln(1+x) / (x² + 1) dx
Little known MySQL import data
Go from introduction to practice -- coordination mechanism (note)
PCIE知识点-008:PCIE switch的结构
Go from introduction to practice - Interface (notes)
Quick excel export
专题教程——选队长游戏
Process control task
List of language weaknesses --cwe, a website worth learning
IO stream code
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
神奇的POI读取excel模板文件报错
Go from introduction to actual combat - panic and recover (notes)
Go从入门到实战——依赖管理(笔记)
SQL必需掌握的100个重要知识点:组合 WHERE 子句