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

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

C++:

方法一:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int findBottomLeftValue(TreeNode* root) {
  13. if(!root)
  14. {
  15. return 0;
  16. }
  17. int max_depth=1,res=root->val;
  18. helper(root,1,max_depth,res);
  19. return res;
  20. }
  21. void helper(TreeNode* node,int depth,int &max_depth,int &res)
  22. {
  23. if(!node)
  24. {
  25. return;
  26. }
  27. if(depth>max_depth)
  28. {
  29. max_depth=depth;
  30. res=node->val;
  31. }
  32. helper(node->left,depth+1,max_depth,res);
  33. helper(node->right,depth+1,max_depth,res);
  34. }
  35. };

方法二:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int findBottomLeftValue(TreeNode* root) {
  13. if(!root)
  14. {
  15. return 0;
  16. }
  17. int res=root->val;
  18. queue<TreeNode*> que;
  19. que.push(root);
  20. while(!que.empty())
  21. {
  22. int n=que.size();
  23. for(int i=0;i<n;++i)
  24. {
  25. root=que.front();
  26. que.pop();
  27. if(i==0)
  28. {
  29. res=root->val;
  30. }
  31. if(root->left)
  32. {
  33. que.push(root->left);
  34. }
  35. if(root->right)
  36. {
  37. que.push(root->right);
  38. }
  39. }
  40. }
  41. return res;
  42. }
  43. };

参考: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. TopSelf安装Windows服务提示:执行未经授权的操作。。

    在一个项目中用到了八九个服务,服务的执行时间也是五花八门,有的年末执行一次,有的月中执行一次,有的月末最后一天执行一次,有的月初连续执行5天, 有的每天晚上执行,...还好各个服务并没有严格的关联关系 ...

  2. MapReduce算法形式三:cleanup

    案例三:cleanup 其实这个案例可以不用写这么复杂,不用cleanup也能写,但是为了,突显,突显,突显(重要的事说四遍)cleanup的重要性,琢磨了半天,恩,这样写既可以突显cleanup又显 ...

  3. G.易彰彪的一张表

    易彰彪最近有点奇怪,一向爱打游戏他最近居然盯着一张全是大小写字母的表在看,好像在找什么东西.他说,这是他女神给他的一张表,他需要回答女神的问题——在忽略大小写(即大写字母和小写字母视为同一字母)的情况 ...

  4. Lesson one of python

    Test1:Use the powershell to output the contents print "Hello World!" print "Hello Aga ...

  5. dba操作之archivelog清理

    下面的命令用于校验归档日志的有效性,列出无效的归档日志,以及以何种方式清除归档日志,列出几种常用的: crosscheck archivelog all;                        ...

  6. skynet源码阅读<2>--网络部分

    先来看下socket_server的数据结构,这里简称为ss: struct socket_server { int recvctrl_fd; int sendctrl_fd; int checkct ...

  7. 汇编环境的搭建(windows 10 + debug)

    1. debug.exe 安装 win10 版本过高,不再提供 debug.exe,甚至从别处获取的 debug.exe 的也无法运行. 汇编语言学习所需的各种执行文件(debug.exe.link. ...

  8. HDU4188:RealPhobia (连分数的运用之一)

    Bert is a programmer with a real fear of floating point arithmetic. Bert has quite successfully used ...

  9. 【扬中集训DAY2T2】 机智的AmyZhi

    [题目链接] 点击打开链接 [算法] 据说标算是暴力? 从N-200开始搜 不过我用了搜索+一些奇怪的剪枝,也A了.... [代码] 标程 #include<bits/stdc++.h> ...

  10. 【213】IDL函数汇总

    名称 功能说明 类型  语法&举例 IDL_VALIDNAME 判断变量名是否有效,无效返回值为空或者自动修改 函数   DEFSYSV 自定义系统变量,全局变量 过程   MAKE_ARRA ...