当前位置:网站首页>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);
}
}
边栏推荐
- Using variables in sed command
- Method of intercepting string in shell
- LinkedList set
- LeetCode 871. 最低加油次数
- JS ternary operator - learning notes (with cases)
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- LeetCode 241. 为运算表达式设计优先级
- Binary to decimal, decimal to binary
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- 树形DP AcWing 285. 没有上司的舞会
猜你喜欢

Drawing maze EasyX library with recursive backtracking method

PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time

20220630学习打卡
![[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle](/img/b7/7bf2a4a9ab51364352aa5e0a196b6d.jpg)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle

LeetCode 75. 颜色分类

Allocation exception Servlet

状态压缩DP AcWing 91. 最短Hamilton路径

LeetCode 871. 最低加油次数

Dom4j遍历和更新XML
随机推荐
[concurrent programming] collaboration between threads
Final review of Database Principles
樹形DP AcWing 285. 沒有上司的舞會
On the difference and connection between find and select in TP5 framework
注解简化配置与启动时加载
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Shell script kills the process according to the port number
Convert video to GIF
Monotonic stack -503 Next bigger Element II
Unity Editor Extension - drag and drop
LeetCode 535. TinyURL 的加密与解密
Binary tree sorting (C language, char type)
Annotations simplify configuration and loading at startup
The method for win10 system to enter the control panel is as follows:
Six dimensional space (C language)
请求参数的发送和接收
Unity multi open script
Deep parsing (picture and text) JVM garbage collector (II)
Es8 async and await learning notes
Allocation exception Servlet