0% found this document useful (0 votes)
25 views

Binary Tree and BST Questions

This document contains code to summarize the top and bottom views of a binary tree. It uses a Pair class to store nodes and their heights. For the top view, it adds the root to a queue with height 0 and maps it to a TreeMap with key 0. It dequeues nodes, adds their children to the queue if their heights are not already in the map, and maps the children. It returns the data of values in the TreeMap. For bottom view, it does the same process but maps all nodes at a height rather than just the first.

Uploaded by

raghavgoel0212
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Binary Tree and BST Questions

This document contains code to summarize the top and bottom views of a binary tree. It uses a Pair class to store nodes and their heights. For the top view, it adds the root to a queue with height 0 and maps it to a TreeMap with key 0. It dequeues nodes, adds their children to the queue if their heights are not already in the map, and maps the children. It returns the data of values in the TreeMap. For bottom view, it does the same process but maps all nodes at a height rather than just the first.

Uploaded by

raghavgoel0212
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Top view of a binary tree

class Pair
{
Node node;
Integer height;
Pair(Node node,Integer height)
{
this.node=node;
this.height=height;
}
}

class Solution
{

static ArrayList<Integer> topView(Node root)


{
Queue<Pair>q1=new LinkedList<>();
ArrayList<Integer>a1=new ArrayList<>();
TreeMap<Integer,Node>treeMap=new TreeMap<>();
if(root==null)
return a1;
q1.add(new Pair(root,0));
treeMap.put(0,root);
while(q1.isEmpty()==false)
{
Pair top=q1.poll();
Node n1=top.node;
int h1=top.height;
if(n1.left!=null)
{
if(treeMap.containsKey(h1-1)==false)
{
treeMap.put(h1-1,n1.left);
}
q1.add(new Pair(n1.left,h1-1));
}
if(n1.right!=null)
{
if(treeMap.containsKey(h1+1)==false)
{
treeMap.put(h1+1,n1.right);
}
q1.add(new Pair(n1.right,h1+1));
}
}
for(Node temp:treeMap.values())
{
a1.add(temp.data);
}
return a1;
}
}
Bottom view of a binary tree

class Pair
{
Node node;
Integer height;
Pair(Node node,Integer height)
{
this.node=node;
this.height=height;
}
}

class Solution
{
//Function to return a list containing the bottom view of the given tree.
public ArrayList <Integer> bottomView(Node root)
{
ArrayList<Integer>a1=new ArrayList<>();
TreeMap<Integer,Node>treeMap=new TreeMap<>();
if(root==null)
return a1;
Queue<Pair>q1=new LinkedList<>();
q1.add(new Pair(root,0));
treeMap.put(0,root);
while(q1.isEmpty()==false)
{
Pair top=q1.poll();
Node n1=top.node;
int h1=top.height;
if(n1.left!=null)
{
if(treeMap.containsKey(h1-1)==false || treeMap.containsKey(h1-
1)==true)
{
treeMap.put(h1-1,n1.left);
}
q1.add(new Pair(n1.left,h1-1));
}
if(n1.right!=null)
{
if(treeMap.containsKey(h1+1)==false ||
treeMap.containsKey(h1+1)==true)
{
treeMap.put(h1+1,n1.right);
}
q1.add(new Pair(n1.right,h1+1));
}
}
for(Node temp:treeMap.values())
{
a1.add(temp.data);
}
return a1;
}
}

You might also like