-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path160-Triangle Min path sum
More file actions
51 lines (49 loc) · 1.35 KB
/
Copy path160-Triangle Min path sum
File metadata and controls
51 lines (49 loc) · 1.35 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
/*
class Node
{
int data; //data of the node
int hd; //horizontal distance of the node
Node left, right; //left and right references
// Constructor of tree node
public Node(int key)
{
data = key;
hd = Integer.MAX_VALUE;
left = right = null;
}
}
*/
class Solution {
static class Pair{
Node node;
int hd;
Pair(Node n, int h){
this.node=n;
this.hd=h;
}
}
static ArrayList<Integer> bottomView(Node root) {
// code here
ArrayList<Integer> result=new ArrayList<>();
if(root==null) return result;
int minHd=0,maxHd=0;
Queue<Pair> queue=new LinkedList<>();
queue.offer(new Pair(root,0));
Map<Integer,Integer> map=new HashMap<>();
while(!queue.isEmpty()){
Pair p=queue.poll();
int hd=p.hd;
Node node=p.node;
map.put(hd,node.data);
if(node.left!=null) queue.offer(new Pair(node.left,hd-1));
if(node.right!=null) queue.offer(new Pair(node.right,hd+1));
minHd=Math.min(minHd,hd);
maxHd=Math.max(maxHd,hd);
}
for(int i=minHd;i<=maxHd;i++){
result.add(map.get(i));
}
return result;
}
https://www.geeksforgeeks.org/problems/bottom-view-of-binary-tree/1
}