当前位置:网站首页>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);
}
}
边栏推荐
- LinkedList set
- Common DOS commands
- DOM render mount patch responsive system
- [concurrent programming] atomic operation CAS
- Message queue for interprocess communication
- 树形DP AcWing 285. 没有上司的舞会
- Concurrent programming (III) detailed explanation of synchronized keyword
- JS non Boolean operation - learning notes
- 我们有个共同的名字,XX工
- 20220630 learning clock in
猜你喜欢
Concurrent programming (VI) ABA problems and solutions under CAS
Drawing maze EasyX library with recursive backtracking method
TP5 multi condition sorting
too many open files解决方案
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Format - C language project sub file
我们有个共同的名字,XX工
How to use Jupiter notebook
状态压缩DP AcWing 291. 蒙德里安的梦想
请求参数的发送和接收
随机推荐
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
[concurrent programming] atomic operation CAS
Common DOS commands
Divide candy (circular queue)
Find the combination number acwing 885 Find the combination number I
Methods of using arrays as function parameters in shell
Log4j2 vulnerability recurrence and analysis
求组合数 AcWing 886. 求组合数 II
[rust notes] 08 enumeration and mode
DOM render mount patch responsive system
[rust notes] 02 ownership
MySQL index types B-tree and hash
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
Collection interface
Monotonic stack -503 Next bigger Element II
Really explain the five data structures of redis
Binary tree sorting (C language, int type)
On the setting of global variable position in C language
JS ternary operator - learning notes (with cases)
Unity Editor Extension - drag and drop