当前位置:网站首页>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);
}
}
边栏推荐
- 【Rust 笔记】13-迭代器(上)
- C language student management system based on linked list, super detailed
- TP5 order multi condition sort
- 树形DP AcWing 285. 没有上司的舞会
- [concurrent programming] concurrent tool class of thread
- 第一个Servlet
- 状态压缩DP AcWing 91. 最短Hamilton路径
- [concurrent programming] atomic operation CAS
- LeetCode 535. TinyURL 的加密与解密
- [rust notes] 08 enumeration and mode
猜你喜欢

Arbre DP acwing 285. Un bal sans patron.

Unity editor expansion - draw lines

22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制

Life cycle of Servlet

Unity Editor Extension - drag and drop

Slice and index of array with data type

Divide candy (circular queue)

求组合数 AcWing 885. 求组合数 I

Complex character + number pyramid

单调栈-84. 柱状图中最大的矩形
随机推荐
Method of intercepting string in shell
[redis] redis persistent RDB vs AOF (source code)
LeetCode 241. 为运算表达式设计优先级
Life cycle of Servlet
Divide candy (circular queue)
Common DOS commands
Get the link behind? Parameter value after question mark
树形DP AcWing 285. 没有上司的舞会
How to use Jupiter notebook
记忆化搜索 AcWing 901. 滑雪
Find the combination number acwing 885 Find the combination number I
Gif remove blank frame frame number adjustment
求组合数 AcWing 885. 求组合数 I
First Servlet
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
如何应对数仓资源不足导致的核心任务延迟
Convert video to GIF
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
[rust notes] 12 closure
Monotonic stack -503 Next bigger Element II