-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubtree.java
More file actions
69 lines (61 loc) · 2 KB
/
Subtree.java
File metadata and controls
69 lines (61 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Definition for a binary tree node
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
// BinaryTree class to build the tree from an array
class BinaryTree {
private int index = -1;
public TreeNode buildTree(int[] nodes) {
index++;
if (index >= nodes.length || nodes[index] == -1) {
return null;
}
TreeNode newNode = new TreeNode(nodes[index]);
newNode.left = buildTree(nodes);
newNode.right = buildTree(nodes);
return newNode;
}
}
public class Subtree {
// Method to check if two trees are identical
public boolean isIdentical(TreeNode root, TreeNode subRoot) {
if (subRoot == null && root == null) {
return true;
}
if (root == null || subRoot == null) {
return false;
}
if (root.val == subRoot.val) {
return isIdentical(root.left, subRoot.left) && isIdentical(root.right, subRoot.right);
}
return false;
}
// Method to check if one tree is a subtree of another
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (subRoot == null) {
return true;
}
if (root == null) {
return false;
}
if (isIdentical(root, subRoot)) {
return true;
}
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
public static void main(String[] args) {
int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1};
BinaryTree tree = new BinaryTree();
TreeNode root = tree.buildTree(nodes);
// Example of using the isSubtree method
Subtree subtreeChecker = new Subtree();
TreeNode subRoot = new TreeNode(2);
subRoot.left = new TreeNode(4);
subRoot.right = new TreeNode(5);
boolean result = subtreeChecker.isSubtree(root, subRoot);
System.out.println("Is subRoot a subtree of root? " + result);
}
}