Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- class node
- {
- int data;
- node left;
- node right;
- node(int d)
- {
- data=d;
- left=right=null;
- }
- }
- public class Main
- {
- node root=null;
- //insert function
- void insertFunction(int value)
- {
- root=insert(root,value);
- }
- node insert(node currentNode,int value)
- {
- if(currentNode==null)
- {
- currentNode=new node(value);
- return currentNode;
- }
- if(value<currentNode.data)
- {
- currentNode.left=insert(currentNode.left,value);
- }
- else
- {
- currentNode.right=insert(currentNode.right,value);
- }
- return currentNode;
- }
- static boolean isSameTree(Main obj1,Main obj2)
- {
- return isSameTreeFunc(obj1.root,obj2.root);
- }
- static boolean isSameTreeFunc(node p,node q)
- {
- if(p==null&& q==null)
- {
- return true;
- }
- if(p==null||q==null)
- return false;
- if(p.data==q.data && isSameTreeFunc(p.left,q.left)
- && isSameTreeFunc(p.right,q.right))
- // {
- return true;
- //}
- return false;
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- //your code here
- Scanner sc=new Scanner(System.in);
- int n1=sc.nextInt();
- Main ob1=new Main();
- int n2=sc.nextInt();
- Main ob2=new Main();
- for(int i=0;i<n1;i++)
- {
- ob1.insertFunction(sc.nextInt());
- }
- for(int i=0;i<n2;i++)
- {
- ob2.insertFunction(sc.nextInt());
- }
- boolean ans=isSameTree(ob1,ob2);
- if(ans)
- {
- System.out.println("YES");
- }
- else
- {
- System.out.println("NO");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement