当前位置:网站首页>LeetCode 513. 找树左下角的值
LeetCode 513. 找树左下角的值
2022-07-03 08:49:00 【Sasakihaise_】

【层次遍历】找最后一层最下面的值即可
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int bfs(TreeNode node){
Queue<TreeNode> queue = new LinkedList();
queue.offer(node);
int ret = node.val;
while(!queue.isEmpty()){
int n = queue.size();
for(var i = 0; i < n; i++){
TreeNode top = queue.poll();
if(i == 0) ret = top.val;
if(top.left != null) queue.offer(top.left);
if(top.right != null) queue.offer(top.right);
}
}
return ret;
}
public int findBottomLeftValue(TreeNode root) {
return bfs(root);
}
}
边栏推荐
- Alibaba canal actual combat
- Complex character + number pyramid
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- 22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
- producer consumer problem
- Slice and index of array with data type
- Solution of 300ms delay of mobile phone
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- [redis] redis persistent RDB vs AOF (source code)
- LeetCode 75. 颜色分类
猜你喜欢

22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism

Complex character + number pyramid

Query XML documents with XPath

Final review of Database Principles
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

Concurrent programming (III) detailed explanation of synchronized keyword

DOM render mount patch responsive system

树形DP AcWing 285. 没有上司的舞会

求组合数 AcWing 885. 求组合数 I
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue
随机推荐
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
分配异常的servlet
The method for win10 system to enter the control panel is as follows:
状态压缩DP AcWing 291. 蒙德里安的梦想
Find the combination number acwing 886 Find the combination number II
【Rust 笔记】13-迭代器(上)
URL backup 1
createjs easeljs
【Rust 笔记】10-操作符重载
[rust notes] 02 ownership
注解简化配置与启动时加载
Notes and bugs generated during the use of h:i:s and y-m-d
Gif remove blank frame frame number adjustment
状态压缩DP AcWing 91. 最短Hamilton路径
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
TP5 order multi condition sort
How to use Jupiter notebook
如何应对数仓资源不足导致的核心任务延迟
高斯消元 AcWing 883. 高斯消元解线性方程组
Drawing maze EasyX library with recursive backtracking method