Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Binary_Search_Tree/sameStructureBtree1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Node
{
int data;
Node left;
Node right;
Node(int data)
{
this.data=data;
left=right=null;
}
}

class BinaryTree{
Node root;
BinaryTree()
{
root = null;
}
BinaryTree(int data)
{
this.root=new Node(data);
}
boolean sameStructure(Node root1, Node root2)
{
if(root1==null && root2==null) return true;
if(root1==null||root2==null) return false;
return sameStructure(root1.left,root2.left)
&& sameStructure(root1.right,root2.right);
}

}

class sameStructureBtree1{
public static void main(String[] args) {

BinaryTree bt=new BinaryTree(2);
bt.root.left=new Node(3);
bt.root.right=new Node(5);
bt.root.left.right=new Node(9);
bt.root.right.left=new Node(7);

BinaryTree bt1=new BinaryTree(2);
bt1.root.left=new Node(3);
bt1.root.right=new Node(5);
System.out.println("sameStructure: "+bt.sameStructure(bt.root, bt1.root));


}
}