[LeetCode] Find Bottom Left Tree Value 寻找最左下树结点的值
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.
这道题让我们求二叉树的最左下树结点的值,也就是最后一行左数第一个值,那么我首先想的是用先序遍历来做,我们维护一个最大深度和该深度的结点值,由于先序遍历遍历的顺序是根-左-右,所以每一行最左边的结点肯定最先遍历到,那么由于是新一行,那么当前深度肯定比之前的最大深度大,所以我们可以更新最大深度为当前深度,结点值res为当前结点值,这样在遍历到该行其他结点时就不会更新结果res了,参见代码如下:
解法一:
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {int max_depth = , res = root->val;
helper(root, , max_depth, res);
return res;
}
void helper(TreeNode* node, int depth, int& max_depth, int& res) {
if (!node) return;
if (depth > max_depth) {
max_depth = depth;
res = node->val;
}
helper(node->left, depth + , max_depth, res);
helper(node->right, depth + , max_depth, res);
}
};
其实这道题用层序遍历更直接一些,因为层序遍历时遍历完当前行所有结点之后才去下一行,那么我们再遍历每行第一个结点时更新结果res即可,根本不用维护最大深度了,参见代码如下:
解法二:
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
int res = ;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
int n = q.size();
for (int i = ; i < n; ++i) {
TreeNode *t = q.front(); q.pop();
if (i == ) res = t->val;
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
return res;
}
};
我们还可以使用个小trick,使得其更加的简洁,由于一般的层序是从左往右的,那么如果我们反过来,每次从右往左遍历,这样就不用检测每一层的起始位置了,最后一个处理的结点一定是最后一层的最左结点,我们直接返回其结点值即可,参见代码如下:
解法三:
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q{{root}};
while (!q.empty()) {
root = q.front(); q.pop();
if (root->right) q.push(root->right);
if (root->left) q.push(root->left);
}
return root->val;
}
};
参考资料:
https://leetcode.com/problems/find-bottom-left-tree-value/
[LeetCode] Find Bottom Left Tree Value 寻找最左下树结点的值的更多相关文章
- [LeetCode]Find Bottom Left Tree Value
Find Bottom Left Tree Value: Given a binary tree, find the leftmost value in the last row of the tre ...
- LeetCode——Find Bottom Left Tree Value
Question Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)
Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
- LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)
513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...
- LN : leetcode 513 Find Bottom Left Tree Value
lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...
- 513. Find Bottom Left Tree Value - LeetCode
Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节 ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
随机推荐
- 移动端H5地图离线瓦片方案
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 移动端的网速和流量耗费是移动开发必须考虑的两个点.常规的瓦片展 ...
- java基础笔记(3)----函数
前言引入函数前,所有的代码都写在main主函数中,代码过多,代码冗余,可读性差. 引入函数后,函数是实现某一特定功能的代码块.一个类中可以定义多个函数,每个函数和main主函数都是并列关系. 函数: ...
- g第十四周,十五周作业
1.数组中偶数的和 #include <stdio.h> int main(){ ; ]; ;i<=;i++) { scanf("%d ",&a[i]); ...
- 201621123060《JAVA程序设计》第十二周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车. 2.1 简述如何 ...
- 201621123050 《Java程序设计》第3周学习总结
1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1.1 写出你 ...
- 雷云Razer Synapse2.0使用测评 -第二次作业
雷蛇云驱动Razer Synapse2.0使用测评 雷蛇(Razer)是全球顶级游戏设备品牌之一,1998年由CEO Min-Liang Tan和Robert "Razerguy" ...
- 【iOS】单元测试
iOS单元测试(作用及入门提升) 字数1704 阅读16369 评论26 喜欢247 由于只是一些简单实用的东西,学学还是挺不错的.其实单元测试用的好,开发起来也会快很多.单元测试对于我目前来说,就是 ...
- Linux下I/O多路转接之select --fd_set
fd_set 你终于还是来了,能看到这个标题进来的,我想,你一定是和我遇到了一样的问题,一样的疑惑,接下来几个小时,我一定竭尽全力,写出我想说的,希望也正是你所需要的: 关于Linux下I/O多路转接 ...
- Centos7 Yum方式安装Mysql7
不废话,直奔主题,可以覆盖安装. 下载并安装MySQL官方的 Yum Repository [root@localhost ~]# wget -i -c http://dev.mysql.com/ge ...
- JAVA_SE基础——23.类的定义
黑马程序员入学blog ... java 面向对象的语言 对象:真实存在的唯一的实物. 比如:我家的狗, 类: 实际就是对某种类型事物的共性属性与行为的抽取. 抽象的概念... 比如说:车 ...