给定一个二叉树,在树的最后一行找到最左边的值。

详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/

C++:

方法一:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
if(!root)
{
return 0;
}
int max_depth=1,res=root->val;
helper(root,1,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+1,max_depth,res);
helper(node->right,depth+1,max_depth,res);
}
};

方法二:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
if(!root)
{
return 0;
}
int res=root->val;
queue<TreeNode*> que;
que.push(root);
while(!que.empty())
{
int n=que.size();
for(int i=0;i<n;++i)
{
root=que.front();
que.pop();
if(i==0)
{
res=root->val;
}
if(root->left)
{
que.push(root->left);
}
if(root->right)
{
que.push(root->right);
}
}
}
return res;
}
};

参考:http://www.cnblogs.com/grandyang/p/6405128.html

513 Find Bottom Left Tree Value 找树左下角的值的更多相关文章

  1. Leetcode513. Find Bottom Left Tree Value找树左下角的值

    给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...

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

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

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

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

  4. Java实现 LeetCode 513 找树左下角的值

    513. 找树左下角的值 给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输 ...

  5. [Swift]LeetCode513. 找树左下角的值 | 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 ...

  6. 领扣(LeetCode)找树左下角的值 个人题解

    给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...

  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] Find Largest Value in Each Tree Row 找树每行最大的结点值

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

随机推荐

  1. hadoop 集群搭建 配置 spark yarn 对效率的提升永无止境

    [手动验证:任意2个节点间是否实现 双向 ssh免密登录] 弄懂通信原理和集群的容错性 任意2个节点间实现双向 ssh免密登录,默认在~目录下 [实现上步后,在其中任一节点安装\配置hadoop后,可 ...

  2. MySQL alter语句

    1:删除列 ALTER TABLE [表名字] DROP [列名称] 2:增加列 ALTER TABLE [表名字] ADD [列名称] INT NOT NULL  COMMENT '注释说明' 3: ...

  3. @Data 注解在实体类的使用可省去生成GET,SET方法

    你有没有觉得在每个实体类中都要生成get,set方法很麻烦, 要是需求的变化,需要再加一个字段,添加属性后则又要添加其get,set方法 现有一个很好用的注解能解决这个麻烦 1,idea中装上 lom ...

  4. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  5. html5--6-50 动画效果-变形

    html5--6-50 动画效果-变形 实例 学习要点 掌握2D变形动画 了解3D变形动画 transform:2D变形: 通过 CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸.转换方 ...

  6. async-await系列翻译(一)

    本篇翻译的英文链接:https://docs.microsoft.com/en-us/dotnet/articles/standard/async-in-depth 使用.NET的基于任务的异步编程模 ...

  7. 【POJ 3580】 SuperMemo

    [题目链接] 点击打开链接 [算法] 本题也是Splay区间操作的模板题,不过要比BZOJ 3223要稍微复杂一些,做完此题后,我终于对Splay有了更深入的理解,有“拨开云雾见青天”的感觉 本题还是 ...

  8. 小K的农场(差分约束)

    题目大意 n个点 m条描述 农场 a 比农场 b 至少多种植了 c 个单位的作物. 农场 a 比农场 b 至多多种植了 c 个单位的作物. 农场 a 与农场 b 种植的作物数一样多. 题解 差分约束裸 ...

  9. unity anim(转)

    Unity4的Mecanim动画很早以前就有体验过,迟迟没有加到项目中有两个原因,今天写这篇博客来记录我在做的过程中遇到的一些问题. 1.以前的代码代码量比较多,修改起来动的地方太多了. 2.使用Me ...

  10. 一个不当使用fclose引发的异常

    最近服务器上一个后台传输文件的服务,经常会报出异常来,只能强行终止并重启. 昨天刚好有空,现场抓了一下dump,再把程序扔到IDA里看了一下,很快就找出原因了,原来是调用fclose时出错的. 使用C ...