当前位置:网站首页>[LeetCode]513. 找树左下角的值
[LeetCode]513. 找树左下角的值
2022-06-27 19:35:00 【阿飞算法】
513. 找树左下角的值
给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
示例 1:
输入: root = [2,1,3]
输出: 1
示例 2:
输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7
提示:
二叉树的节点个数的范围是 [1,104]
-231 <= Node.val <= 231 - 1
方法1:BFS
- 层序遍历,迭代每一层,取最左边的
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;
}
方法2:DFS
- 先左子树节点后右子树节点,左子树切换到右子树的时机进行最大深度
maxDepth的更新与记录,并保存结果值
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:找树右下角的值
方法1:BFS
- 记录每一层的最后一个值
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;
}
方法2:DFS
- 先右子树后左子树
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);
}
边栏推荐
- Quick excel export
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
- Quick excel export according to customized excel Title Template
- Go from introduction to actual combat - context and task cancellation (notes)
- Educational Codeforces Round 108 (Rated for Div. 2)
- 跟我一起AQS SOS AQS
- Day8 ---- 云资讯项目介绍与创建
- Flask----应用案例
- win11桌面出現“了解此圖片”如何删除
- Array assignment
猜你喜欢

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

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

Codeforces Round #716 (Div. 2)

Go from introduction to actual combat - package (notes)

AI painting minimalist tutorial

Process control task

100 important knowledge points that SQL must master: using functions to process data

创建对象时JVM内存结构

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
随机推荐
Use the storcli tool to configure raid. Just collect this article
神奇的POI读取excel模板文件报错
流程控制任务
Go从入门到实战——任务的取消(笔记)
Go从入门到实战——Panic和recover(笔记)
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
Go from introduction to practice -- coordination mechanism (note)
Special training of guessing game
Codeforces Round #719 (Div. 3)
猜拳游戏专题训练
Oracle migration MySQL unique index case insensitive don't be afraid
Go从入门到实战——错误机制(笔记)
Quick excel export according to customized excel Title Template
MySQL usage notes 1
White whoring red team goby & POC, how do you call white whoring?
Process control task
OpenSSL 编程 二:搭建 CA
gomock mockgen : unknown embedded interface
Go from starting to Real - Interface (note)
让马化腾失望了!Web3.0,毫无希望