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

详见: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. 转载-STM32片上FLASH内存映射、页面大小、寄存器映射

    原文地址:http://blog.chinaunix.net/uid-20617446-id-3847242.html 本文以STM32F103RBT6为例介绍了片上Flash(Embedded Fl ...

  2. Vue中 key keep-alive

    keep-alive key <!DOCTYPE html> <html> <head> <title></title> <scrip ...

  3. bzoj2436: [Noi2011]Noi嘉年华

    我震惊了,我好菜,我是不是该退役(苦逼) 可以先看看代码里的注释 首先我们先考虑一下第一问好了真做起来也就这个能想想了 那么离散化时间是肯定的,看一手范围猜出是二维DP,那对于两个会场,一个放自变量, ...

  4. javascript 树形菜单

    1. [代码][JavaScript]代码     var ME={ini:{i:true,d:{},d1:{},h:0,h1:0,h2:0},html:function(da,f){var s='& ...

  5. Linux:外网域名防火墙设置导致下载失败

    问题现象: 通过IE从服务器下载文件时,提示Can't read from connection: Connection reset by peer. 别的现场都是好的,只有该现场有这个问题.所以,一 ...

  6. 【原】WPF客户端只能启动一次

    public partial class App : Application { System.Threading.Mutex mutex; public App() { this.Startup + ...

  7. 如何下载WDK

    随着Windows Vista和Windows Server 2008的相继发布,微软的驱动开发工具也进行了相应的更新换代.原来的驱动开发工具包叫做DDK(Driver Develpment Kit) ...

  8. ASP.NET Core:WebAppCoreRESTful

    ylbtech-ASP.NET Core:WebAppCoreRESTFul 1.返回顶部 1. 2. 3.         4. 2. Controllers返回顶部 1.HomeControlle ...

  9. Linux限制端口

    设置防火墙 iptables -a input -p 协议 -s 可以访问ip -dport端口 -j ACCEPT

  10. Ruby Proc 和 lambda的共同点和区别

    Proc 和 lambda 的目的是把block {....} 变成类似方法一样的对象,使其不需要重复编写同样的block. Proc 和 lambda 的共同点: 语法类似Proc.new{|n| ...