LeetCode link

first thought

  • BFS. Two queue to iterate, update the result when finish one level.

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int result = root.val;
while (!queue.isEmpty()) {
Queue<TreeNode> queue2 = new LinkedList<>();
TreeNode node = queue.peek();
result = node.val;
while (!queue.isEmpty()) {
node = queue.poll();
if (node.left != null) {
queue2.add(node.left);
}
if (node.right != null) {
queue2.add(node.right);
}
}
queue = queue2;
}
return result;
}
}