Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Atharv2433 authored Sep 23, 2024
1 parent f6ec7da commit cf87e01
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Node_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class Node_2{
int data;
Node_2 left, right;

Node_2(int val) {
data = val;
left = right = null;
}

public static boolean is_identical(Node_2 root, Node_2 subroot){
if(root == null && subroot == null){
return true;
}
if(root == null || subroot == null || root.data != subroot.data){
return false;
}
return is_identical(root.left,subroot.left) || is_identical(root.right,subroot.right);

}
public static boolean is_subtree(Node_2 root, Node_2 subroot){
if(root == null){
return false;
}
if(root.data == subroot.data){
if(is_identical(root,subroot)){
return true;
}
}
return is_subtree(root.left,subroot) || is_subtree(root.right,subroot);
}
public static void main(String[] args) {
Node_2 root = new Node_2(1);
root.left = new Node_2(2);
root.right = new Node_2(3);
root.left.left = new Node_2(4);
root.left.right = new Node_2(5);

Node_2 subroot = new Node_2(2);
subroot.left = new Node_2(4);
subroot.right = new Node_2(5);

System.out.println(is_subtree(root, subroot));
}
}

0 comments on commit cf87e01

Please sign in to comment.