-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBottomLeftValue.java
More file actions
52 lines (41 loc) · 862 Bytes
/
BottomLeftValue.java
File metadata and controls
52 lines (41 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package tree;
import java.util.*;
public class BottomLeftValue {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public int findBottomLeftValue(TreeNode root) {
// if(root==null)
// return null;
int rv = root.val;
LinkedList<TreeNode> list = new LinkedList<>();
list.addLast(root);
list.addLast(null);
boolean needToSet = false;
while (list.size() != 1) {
TreeNode node = list.removeFirst();
if (node == null) {
needToSet = true;
list.addLast(node);
} else {
if (needToSet) {
needToSet = false;
rv = node.val;
}
if (node.left != null)
list.addLast(node.left);
if (node.right != null)
list.addLast(node.right);
}
}
return rv;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}