Skip to content

Commit 566f32e

Browse files
Find Bottom Left Tree Value: Accepted
1 parent 59dd54a commit 566f32e

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ My accepted leetcode solutions to some of the common interview problems.
201201
- [Binary Tree Inorder Traversal](problems/src/tree/BinaryTreeInorderTraversal.java) (Medium)
202202
- [Symmetric Tree](problems/src/tree/SymmetricTree.java) (Easy)
203203
- [Maximum Binary Tree](problems/src/tree/MaximumBinaryTree.java) (Medium)
204+
- [Find Bottom Left Tree Value](problems/src/tree/FindBottomLeftTreeValue.java) (Medium)
204205

205206
#### [Two Pointers](problems/src/two_pointers)
206207

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package tree;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 30/08/2017.
5+
* Given a binary tree, find the leftmost value in the last row of the tree.
6+
7+
Example 1:
8+
Input:
9+
10+
2
11+
/ \
12+
1 3
13+
14+
Output:
15+
1
16+
Example 2:
17+
Input:
18+
19+
1
20+
/ \
21+
2 3
22+
/ / \
23+
4 5 6
24+
/
25+
7
26+
27+
Output:
28+
7
29+
Note: You may assume the tree (i.e., the given root node) is not NULL.
30+
31+
32+
*/
33+
public class FindBottomLeftTreeValue {
34+
private int max = 0, result;
35+
public static class TreeNode {
36+
int val;
37+
TreeNode left;
38+
TreeNode right;
39+
TreeNode(int x) { val = x; }
40+
}
41+
42+
public static void main(String[] args) throws Exception{
43+
TreeNode root = new TreeNode(1);
44+
root.left = new TreeNode(2);
45+
root.left.left = new TreeNode(4);
46+
root.right = new TreeNode(3);
47+
root.right.left = new TreeNode(5);
48+
root.right.left.left = new TreeNode(7);
49+
root.right.right = new TreeNode(6);
50+
System.out.println(new FindBottomLeftTreeValue().findBottomLeftValue(root));
51+
}
52+
53+
public int findBottomLeftValue(TreeNode root) {
54+
preorder(root, 1);
55+
return result;
56+
}
57+
58+
private void preorder(TreeNode node, int level){
59+
if(node != null){
60+
if(level > max){
61+
result = node.val;
62+
max = level;
63+
}
64+
preorder(node.left, level + 1);
65+
preorder(node.right, level + 1);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)