码迷,mamicode.com
首页 > 其他好文 > 详细

Find Bottom Left Tree Value

时间:2017-02-17 13:04:25      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:null   tput   not   row   tree   binary   amp   ast   bottom   

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 }

 

Find Bottom Left Tree Value

标签:null   tput   not   row   tree   binary   amp   ast   bottom   

原文地址:http://www.cnblogs.com/amazingzoe/p/6409259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!