Given a binary tree, find the leftmost value in the last row of the tree.

Example 1:

  1. Input:
  2.  
  3. 2
  4. / \
  5. 1 3
  6.  
  7. Output:
  8. 1

Example 2:

  1. Input:
  2.  
  3. 1
  4. / \
  5. 2 3
  6. / / \
  7. 4 5 6
  8. /
  9. 7
  10.  
  11. Output:
  12. 7

Note: You may assume the tree (i.e., the given root node) is not NULL.

这个题的思路其实跟[LeetCode] 199. Binary Tree Right Side View_ Medium tag: BFS, Amazon里面我提到的left side view一样的思路, 只是返回的时候返回最后一个元素即可.

1. Constraints

1) root cannot be None, 所以edge case就是 1

2, Ideas

BFS:       T: O(n),   S: O(n)    n is the number of the nodes of the tree

3. Code

  1. class Solution:
  2. def LeftViewMost(self, root):
  3. ans, queue = [], collections.deque([(root, 0)])
  4. while queue:
  5. node, heig = queue.popleft()
  6. if heig == len(ans):
  7. ans.append(node.val)
  8. if node.left:
  9. queue.append(node.left)
  10. if node.right:
  11. queue.append(node.right)
  12. return ans[-1]

2)

  1. class Solution(object):
  2. def findBottomLeftValue(self, root):
  3. """
  4. :type root: TreeNode
  5. :rtype: int
  6. """
  7. ans, queue = (root.val, 0), collections.deque([(root, 0)])
  8. while queue:
  9. node, height = queue.popleft()
  10. if height > ans[1]:
  11. ans = (node.val, height)
  12. if node.left:
  13. queue.append((node.left, height + 1))
  14. if node.right:
  15. queue.append((node.right, height + 1))
  16. return ans[0]

4. Test case

1) root is 1

2)

  1. 2
  2. / \
  3. 1 3
  4.  
  5. 3)
  1. 1
  2. / \
  3. 2 3
  4. / / \
  5. 4 5 6
  6. /
  7. 7

[LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS的更多相关文章

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

  2. [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS

    Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...

  3. 513. Find Bottom Left Tree Value - LeetCode

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

  4. [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  5. 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...

  6. 【leetcode】513.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 ...

  7. [LeetCode] 199. Binary Tree Right Side View_ Medium tag: BFS, Amazon

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  8. 513 Find Bottom Left Tree Value 找树左下角的值

    给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...

  9. [LC] 513. 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 ...

随机推荐

  1. read by other session 等待事件。

    今天是2014-01-06,从今天开始,打算春节之前每天学习一个等待事件,今天就记录一下read by other session这个等待事件笔记. 什么是read by other session? ...

  2. iPhone X的缺口和CSS

    苹果公司(Apple)的发布会也开完了,新产品也将登陆了.估计很多开发人员看到iPhone X的设备是要崩溃了,特别对于前端开发人员更是如此. iPhone X的屏幕覆盖了整个手机的屏幕,为相机和其他 ...

  3. Learning Git by Animations

    https://hujiaweibujidao.github.io/blog/2016/05/20/learning-git-by-animations/ http://learngitbranchi ...

  4. Panel结构

    参考weui的Panel结构 核心:定位,补充:对容器设置font-size:0,消除img下多出的3px,防止居中出现偏差. <!DOCTYPE html> <html lang= ...

  5. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

  6. 部署OpenStack问题汇总(七)--解决apache启动错误"httpd:Could not reliably determine..."

    今天在调试openstack的时候,重启apache,出现以下报错: [root@hctrl log]# service httpd restart 停止 httpd:[确定] 正在启动 httpd: ...

  7. windows中安装pip,setuptools,django等

    1,安装Python3.6 (下载exe文件,双击安装)      注意设置环境变量,让Python的在任意位置都可以执行 .Python 下载地址:https://www.python.org/do ...

  8. [压缩]C#下使用SevenZipSharp压缩解压文本

    using SevenZip; using System; using System.Collections.Generic; using System.IO; using System.Linq; ...

  9. mysql补充(4)数据完整性

    数据完整性(Data Integrity)是指数据的精确性(Accuracy) 和可靠性(Reliability).(补充mysql数据完整性和约束) 它是应防止数据库中存在不符合语义规定的数据和防止 ...

  10. mysql rowid实现

    ) a, b b表是数据表