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

求二叉树左下方节点的值,使用广度优先遍历,每遍历一层刷新result值

  1. class Solution(object):
  2. def findBottomLeftValue(self, root):
  3. """
  4. :type root: TreeNode
  5. :rtype: int
  6. """
  7. left = 0
  8. if(not root):
  9. return left
  10. result = []
  11. queue = [root]
  12. while(queue):
  13. count = len(queue)
  14. for i in range(0, count):
  15. node = queue.pop(0)
  16. if i == 0:
  17. left = node.val
  18. if(node.left):
  19. queue.append(node.left)
  20. if(node.right):
  21. queue.append(node.right)
  22. return left

来自为知笔记(Wiz)

时间: 2024-08-03 09:15:11

513. Find Bottom Left Tree Value 二叉树左下节点的值的相关文章

513. Find Bottom Left Tree Value - LeetCode

Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节点就是该层最左侧的节点 Java实现: public int findBottomLeftValue(TreeNode root) { Queue<TreeNode> nodeQueue = new LinkedList<>(); nodeQueue.offer(root); // 向

[LeetCode]513 Find Bottom Left Tree Value(BFS)

题目链接:https://leetcode.com/problems/find-bottom-left-tree-value/?tab=Description 题意:找到二叉树里最底下的最靠左的叶子节点的值. 看到input的时候吓哭了,以为是要处理这种输入.看到代码填空部分才缓过来- 直接bfs,遇到更深的就更新,每次都让最左的先入队列.就能保证每次更新到的答案都是最左的. 1 /** 2 * Definition for a binary tree node. 3 * struct Tree

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 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. 分析:广搜,层序遍历,保存每层的第一个

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 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. 现在开始考bfs 了吗, 根据size

[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 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. Solution 1: DFS, pr

leetcode算法: Find Bottom Left Tree Value

leetcode算法: 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 3 Output:1Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output:7Note: You may assume the tree (i.e., the given ro

LeetCode:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

POJ 2255 Tree Recovery 二叉树恢复

一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求根据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 不过其实算法都是一样的.仿佛又回到了做Leetcode题的那段岁月中了. 还有就是输入是我特别处理过的,就两个函数,大家会了的无视,不会的可以学习下. #include <stdio.h> #include <string> #include <algorithm> using

[LintCode] Invert Binary Tree 翻转二叉树

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So