forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindBottomLeftTreeValue.java
More file actions
41 lines (30 loc) · 913 Bytes
/
FindBottomLeftTreeValue.java
File metadata and controls
41 lines (30 loc) · 913 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
import java.util.LinkedList;
import java.util.Queue;
public class FindBottomLeftTreeValue {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
Queue<TreeNode> next = new LinkedList<>();
TreeNode target = null;
queue.offer(root);
boolean firstPoll = true;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (firstPoll) {
firstPoll = false;
target = node;
}
if (node.left != null) {
next.offer(node.left);
}
if (node.right != null) {
next.offer(node.right);
}
if (queue.isEmpty()) {
queue.addAll(next);
next.clear();
firstPoll = true;
}
}
return target.val;
}
}