-
Notifications
You must be signed in to change notification settings - Fork 0
/
CN Bottom View Of Binary Tree - recursion.java
50 lines (42 loc) · 1.21 KB
/
CN Bottom View Of Binary Tree - recursion.java
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
/*********************************************
Following is the TreeNode class structure
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
*********************************************/
import java.util.*;
class Pair {
int level;
int nodeVal;
Pair (int level, int nodeVal) {
this.level = level;
this.nodeVal = nodeVal;
}
}
public class Solution {
public static List<Integer> bottomView(TreeNode root) {
// Write your code here.
TreeMap<Integer, Pair> map = new TreeMap<>();
travel(0, 0, root, map);
List<Integer> ans = new ArrayList<>();
for (Pair pair: map.values())
ans.add(pair.nodeVal);
return ans;
}
static void travel(int x, int y, TreeNode cur, TreeMap<Integer, Pair> map) {
if (cur == null) return;
if (!map.containsKey(x))
map.put(x, new Pair(y, cur.val));
else if(map.get(x).level <= y)
map.put(x, new Pair(y, cur.val));
travel(x - 1, y + 1, cur.left, map);
travel(x + 1, y + 1, cur.right, map);
}
}