Bottom view of a binary tree using dfs
		```
		
		lass Solution

{
//Function to return a list containing the bottom view of the given tree.
public ArrayList bottomView(Node root)
{
// Code here
ArrayList ans = new ArrayList<>();
if(root==null)
return ans;
ArrayList list = new ArrayList<>();
helper(new Pair(0,0,root),list);
Collections.sort(list,(p1,p2) ->p1.pos==p2.pos?p2.y-p1.y:p1.pos-p2.pos);
Map<Integer,Integer> map = new TreeMap<>();
for(Pair p : list){
map.put(p.pos,p.root.data);

    }
    for(int en : map.values())
    ans.add(en);
    return ans;
    
    
    
    
}
static void helper(Pair p,ArrayList<Pair> list ){
    list.add(p);
    if(p.root.left!=null){
        helper(new Pair(p.pos-1,p.y-1,p.root.left), list);
    }
     if(p.root.right!=null){
        helper(new Pair(p.pos+1,p.y-1,p.root.right),list);
    }
}
static class Pair{
    int pos ;
    int y; 
    Node root;
    Pair(int pos,int y ,Node root){
        this.pos = pos;
        this.y =y;
        this.root=root;
    }
}

}
```

Comments (1)