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

Codeforces Round #716 (Div. 2)

100 important knowledge points that SQL must master: sorting and retrieving data

Go从入门到实战——Context与任务取消(笔记)

Go从入门到实战——package(笔记)

【MySQL】数据库函数通关教程下篇(窗口函数专题)

图解基于AQS队列实现的CountDownLatch和CyclicBarrier

抖音的兴趣电商已经碰到流量天花板?

AI 绘画极简教程

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
![[LeetCode]动态规划解分割数组II[Arctic Fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[LeetCode]动态规划解分割数组II[Arctic Fox]
随机推荐
Codeforces Round #719 (Div. 3)
Go从入门到实战——错误机制(笔记)
[LeetCode]508. 出现次数最多的子树元素和
win11桌面出现“了解此图片”如何删除
Quick excel export
SQL必需掌握的100个重要知识点:IN 操作符
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
Icml2022 | scalable depth Gaussian Markov random field
100 important knowledge points that SQL must master: combining where clauses
Go从入门到实战——Panic和recover(笔记)
∫(0→1) ln(1+x) / (x² + 1) dx
Flask----应用案例
AI 绘画极简教程
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
[LeetCode]508. The most frequent subtree elements and
Go从入门到实战——仅需任意任务完成(笔记)
SQL必需掌握的100个重要知识点:检索数据
根据自定义excel标题模板快速excel导出
ICML2022 | 可扩展深度高斯马尔可夫随机场
让马化腾失望了!Web3.0,毫无希望