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/

https://leetcode.com/problems/find-bottom-left-tree-value/discuss/98779/Right-to-Left-BFS-(Python-%2B-Java)

https://leetcode.com/problems/find-bottom-left-tree-value/discuss/98786/verbose-java-solution-binary-tree-level-order-traversal

[LeetCode] Find Bottom Left Tree Value 寻找最左下树结点的值的更多相关文章

  1. [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 ...

  2. 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: ...

  3. [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 ...

  4. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

  5. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...

  6. LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)

    513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...

  7. 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 ...

  8. 513. Find Bottom Left Tree Value - LeetCode

    Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节 ...

  9. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

随机推荐

  1. java中最常用jar包的用途说明

    java中最常用jar包的用途说明,适合初学者 jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实 ...

  2. springMVC框架+POI组件导出Excel

    目的:访问url(http://localhost:8080/POIOutputExcel/outputexcel.do)实现excel导出,效果图如下: 文件目录(配置文件就不多说了,跟前面的随笔一 ...

  3. Online database documentation.

    贫道2018年1月正式出道,可以说在IT界我就是个菜鸟.但我有着一颗不服输的心,我相信我会在这条路走上巅峰之道的.下面我来写我的第一份学习笔记: 介绍:大多数公司都有自己的数据文档,估计大多数都是用P ...

  4. 2017-2018-1 1623 bug终结者 冲刺005

    bug终结者 冲刺005 by 20162323 周楠 今日任务:理清游戏运行逻辑,GameView类为游戏核心代码 简要介绍 游戏中整个地图都是由数组组成 1.整个地图为16×16格,主要元素有墙. ...

  5. Twisted 介绍 及TCP广播系统实例

    twisted 提供更多传输层 udp,tcp,tls及应用层HTTP,FTP等协议的支持,在开发方法上更提供了丰富的特性来支持异步编程 安装twisted 建议使用anaconda 安装,conda ...

  6. jstree的简单用法

    一般我们用jstree主要实现树的形成,并且夹杂的邮件增删重命名刷新的功能 下面是我在项目中的运用,采用的是异步加载 $('#sensor_ul').data('jstree', false).emp ...

  7. php函数var_dump() 、print_r()、echo()

    var_dump() 能打印出类型 print_r() 只能打出值 echo() 是正常输出... 需要精确调试的时候用 var_dump(); 一般查看的时候用 print_r() 另外 , ech ...

  8. Linq 延迟加载

    IList<Student> ssList = new List<Student>() { , StudentName = "John", } , , St ...

  9. python 内置函数之lambda-filter-reduce-apply-map

    (1)lambda lambda是Python中一个很有用的语法,它允许你快速定义单行最小函数.类似于C语言中的宏,可以用在任何需要函数的地方. 基本语法如下: 函数名 = lambda args1, ...

  10. 【漏洞复现】PHPCMS wap模块 SQL注入(附EXP)

    漏洞影响版本:v9.5.8.v9.6.0 Step1: 访问:http://www.xxx.com/index.php?m=wap&a=index&siteid=1, 获取返回的coo ...