-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6ec7da
commit cf87e01
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |