【leetcode】513.Find Bottom Left Tree Value
2024-09-26 10:28:24
原题
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:
2
/
1 3
Output:
1
Example 2:
Input:
1
/ \
2 3
/ / \
4 5 6
/
7
Output:
7
Note: You may assume the tree (i.e., the given root node) is not NULL.
解析
查找最后一层最左侧的叶子节点的值
思路
BFS,从右向左搜索
解法(同最优解)
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
TreeNode last = root;
queue.offer(root);
while (!queue.isEmpty()) {
last = queue.poll();
if (last.right != null) {
queue.offer(last.right);
}
if (last.left != null) {
queue.offer(last.left);
}
}
return last.val;
}
最新文章
- 自学H5第一天笔记
- Space.js – HTML 驱动的页面 3D 滚动效果
- RMQ(ST算法)
- enmo_day_07
- WW_TRANS_I18N_LOCALE”与“WW_TRANS_I18N_LOCALE”属性
- python (10) 文件夹的创建与文件夹的删除
- android通过httpClient请求获取JSON数据并且解析
- Nginx vs Apache--reference
- 完善GDAL与OpenCV间的数据格式转换与影像分块读写
- iOS中分段控制器与UIScrollView结合使用
- element-ui和npm、webpack、vue-cli搭建Vue项目
- freeMark模板引擎
- nethogs命令执行报异常的解决方法
- Servlet 2.0 &;&; Servlet 3.0 新特性
- L328 What Is Millennial Burnout?
- Visual Studio2013的安装过程及练习测试
- Use Memory Layout from Target Dialog Scatter File
- 软件产品案例分析——福州大学微信小程序
- alter system set events相关知识
- [翻译] SCRecorder