05 August 2008
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
1
2
3
4
5
6
7
8
Input:
2
/ \
1 3
Output:
1
Example 2:
1
2
3
4
5
6
7
8
9
10
11
12
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.
BFS记录该层第一个;或者DFS记录最大层数,判断第一次进入该层的节点。
BFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
root = queue.poll();
// 先存右边,方便取左边
if (root.right != null) {
queue.offer(root.right);
}
if (root.left != null) {
queue.offer(root.left);
}
}
return root.val;
}
}
DFS
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int result;
int maxDepth;
public int findBottomLeftValue(TreeNode root) {
result = 0;
maxDepth = 0;
helper(root, 1);
return result;
}
private void helper(TreeNode node, int depth) {
if (node == null) {
return;
}
// 未达到该层时maxDepth < depth,只要遍历到该层第一个数后就为maxDepth == depth了
if (maxDepth < depth) {
maxDepth = depth;
result = node.val;
}
depth++;
helper(node.left, depth);
helper(node.right, depth);
}
}