当前位置:网站首页>Leetcode513.找出树的左下角的值
Leetcode513.找出树的左下角的值
2022-06-26 05:17:00 【Java全栈研发大联盟】

解题思路:
题目要求的值肯定是在最后一层上,这是肯定的。 所以我们只需要找到最后一层的节点。然后再想办法就行。
那么对于这么一棵树,咱们层序遍历的话,其实可以从树的右子树开始遍历,再遍历左子树,那么咱们在队列出队的最后一个元素的时候, 就是咱们要找的最深一层的最左边的一个元素的。
有没有感觉,对于这个题来说,广度优先算法,更加容易和适合
代码如下:
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
root = queue.poll();
if (root.right != null) queue.offer(root.right);
if (root.left != null) queue.offer(root.left);
}
return root.val;
}
}
边栏推荐
- Sentimentin tensorflow_ analysis_ cell
- Schematic diagram of UWB ultra high precision positioning system
- Create a binary response variable using the cut sub box operation
- Excellent learning ability is your only sustainable competitive advantage
- 瀚高数据库自定义操作符‘!~~‘
- Status of processes and communication between processes
- localStorage浏览器本地储存,解决游客不登录的情况下限制提交表单次数。
- Official image acceleration
- [upsampling method opencv interpolation]
- ThreadPoolExecutor implements file uploading and batch inserting data
猜你喜欢

Tensorflow and deep learning day 3

Install the tp6.0 framework under windows, picture and text. Thinkphp6.0 installation tutorial

LeetCode 19. 删除链表的倒数第 N 个结点
The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of

ModuleNotFoundError: No module named ‘numpy‘

SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor

86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)

Guanghetong and anti international bring 5g R16 powerful performance to the AI edge computing platform based on NVIDIA Jetson Xavier nx

【Unity3D】碰撞体组件Collider

cartographer_fast_correlative_scan_matcher_2d分支定界粗匹配
随机推荐
Fedora alicloud source
Pycharm package import error without warning
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
PHP之一句话木马
Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
Keras actual combat cifar10 in tensorflow
tensorlow:cifar100_ train
A beginner's entry is enough: develop mobile IM from zero
Gd32f3x0 official PWM drive has a small positive bandwidth (inaccurate timing)
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
Mongodb image configuration method
二次bootloader关于boot28.asm应用的注意事项,28035的
data = self._ data_ queue. get(timeout=timeout)
Cookie and session Basics
Codeforces Round #800 (Div. 2)
Codeforces Round #802 (Div. 2)(A-D)
Tp5.0 framework PDO connection MySQL error: too many connections solution
cartographer_optimization_problem_2d
Tensorflow visualization tensorboard "no graph definition files were found." error
6.1 - 6.2 Introduction à la cryptographie à clé publique