-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeBottomView.java
More file actions
60 lines (59 loc) · 1.77 KB
/
BinaryTreeBottomView.java
File metadata and controls
60 lines (59 loc) · 1.77 KB
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
53
54
55
56
57
58
59
60
class Tree
{
ArrayList<Integer> sol=new ArrayList<>();
ArrayList<Node> level=new ArrayList<>();
HashMap<Node,Integer> numbering=new HashMap<>();
TreeMap<Integer,List<Integer>> treemap=new TreeMap<>();
public ArrayList <Integer> bottomView(Node root)
{
numbering(root,0);
levelorder(root);
for(Node k:level){
if(treemap.containsKey(numbering.get(k))){
List<Integer> temp=treemap.get(numbering.get(k));
temp.add(k.data);
treemap.put(numbering.get(k),temp);
}
else{
List<Integer> temp=new ArrayList<Integer>();
temp.add(k.data);
treemap.put(numbering.get(k),temp);
}
}
for(int val:treemap.keySet()){
sol.add(treemap.get(val).get(treemap.get(val).size()-1));
}
return sol;
}
private void levelorder(Node root){
if(root==null)return;
Deque<Node> q=new ArrayDeque<>();
q.add(root);
while(!q.isEmpty()){
int qlen=q.size();
List<Node> subsol=new ArrayList<>();
for(int i=0;i<qlen;i++){
Node curr=q.poll();
subsol.add(curr);
if(curr.left!=null){
q.add(curr.left);
}
if(curr.right!=null){
q.add(curr.right);
}
}
level.addAll(subsol);
}
}
private void numbering(Node root,int hd){
if(root==null)return;
if(root!=null)
numbering.put(root,hd);
if(root.right!=null){
numbering(root.right,hd+1);
}
if(root.left!=null){
numbering(root.left,hd-1);
}
}
}