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.

这个题的思路其实跟[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

 class Solution:
def LeftViewMost(self, root):
ans, queue = [], collections.deque([(root, 0)])
while queue:
node, heig = queue.popleft()
if heig == len(ans):
ans.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return ans[-1]

2)

class Solution(object):
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
ans, queue = (root.val, 0), collections.deque([(root, 0)])
while queue:
node, height = queue.popleft()
if height > ans[1]:
ans = (node.val, height)
if node.left:
queue.append((node.left, height + 1))
if node.right:
queue.append((node.right, height + 1))
return ans[0]

4. Test case

1) root is 1

2)

    2
/ \
1 3 3)
        1
/ \
2 3
/ / \
4 5 6
/
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. Win7/Win8/IIS7/IIS8配置ASP/ACCESS

    1.在IIS信息服务管理器配置好站点后,配置ASP属性: a.IIS启用ASP 1.打开控制面板>>程序和功能>>“打开或关闭windows功能”,见下图 2.稍等片刻,出现一 ...

  2. 【大数据系列】hadoop单节点安装官方文档翻译

    Hadoop: Setting up a Single Node Cluster. HADOOP:建立单节点集群 Purpose Prerequisites Supported Platforms R ...

  3. SharpGL学习笔记(一) 平台构建与Opengl的hello World

    (一)平台构建与Opengl的hello World OpenGL就是3d绘图的API,微软针和它竞争推出D3D,也就是玩游戏时最常见的DirectorX组件中的3d功能. 所以不要指望windows ...

  4. Oracle 学习之exists

    不相关子查询:子查询的查询条件不依赖于父查询的称为不相关子查询.相关子查询:子查询的查询条件依赖于外层父查询的某个属性值的称为相关子查询,带EXISTS 的子查询就是相关子查询EXISTS表示存在量词 ...

  5. libevent安装方法

    安装FastDFS之前,先安装libevent工具包,记录一下安装过程 1.检查:ls -al /usr/lib | grep libevent 查看是否已安装,如果已安装且版本低于1.3,则先通过: ...

  6. Redmine插件的安装与卸载,知识库插件安装。

    本文介绍linux版本的Redmine插件安装,通常Redmine安装在Linux系统,/var/www/redmine/路径. 安装: 复制插件到 2.X版本 #{RAILS_ROOT}/plugi ...

  7. 【BZOJ4355】Play with sequence 线段树

    [BZOJ4355]Play with sequence Description 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a ...

  8. Android 应用内切换语言

    extends :http://bbs.51cto.com/thread-1075165-1.html,http://www.cnblogs.com/loulijun/p/3164746.html 1 ...

  9. python3安装builtwith

    >>> import builtwith Traceback (most recent call last): File , in <module> File excep ...

  10. 9.11 Django视图 view和路由

    2018-9-11 16:34:16 2018-9-11 19:00:24 越努力,.越幸运! Django框架参考: https://www.cnblogs.com/liwenzhou/p/8296 ...