Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Created April 21, 2018 08:26
Show Gist options
  • Select an option

  • Save Kishy-nivas/f52baa7a4738e388af41d00d027dfbc3 to your computer and use it in GitHub Desktop.

Select an option

Save Kishy-nivas/f52baa7a4738e388af41d00d027dfbc3 to your computer and use it in GitHub Desktop.
class GfG
{
public HashSet<Integer> set = new HashSet<Integer>();
public void printCommon(Node root1,Node root2){
inOrder(root1);
print(root2);
//System.out.println();
}
public void inOrder(Node root){
if(root == null)
return;
inOrder(root.left);
set.add(root.data);
inOrder(root.right);
}
public void print(Node root){
if(root == null){
return;
}
print(root.left);
if(set.contains(root.data)){
System.out.print(root.data + " ");
}
print(root.right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment