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.

Hide Company Tags

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     private int result = 0, height = 0;
12     public int findBottomLeftValue(TreeNode root) {
13         helper(root, 1);
14         return result;
15     }
16
17     private void helper(TreeNode root, int depth) {
18         if (root == null) return;
19
20         if (height < depth) {
21             result = root.val;
22             height = depth;
23         }
24
25         helper(root.left, depth + 1);
26         helper(root.right, depth + 1);
27     }
28 }
时间: 2024-10-27 14:18:31

Find Bottom Left Tree Value的相关文章

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

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); // 向

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. 求二叉树左下方节点的值,使用广度优先遍

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

LeetCode: Find Bottom Left Tree Value

1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public int findBottomLeftValue(TreeNode root) { 12

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

[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

[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

129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T