Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 60354ea

Browse filesBrowse files
committed
Subtree check solution updated with correct implementation
1 parent 4ea314d commit 60354ea
Copy full SHA for 60354ea

File tree

Expand file treeCollapse file tree

2 files changed

+14
-21
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+14
-21
lines changed
Open diff view settings
Collapse file

‎src/main/java/com/eprogrammerz/examples/algorithm/trees/CheckSubtree.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/eprogrammerz/examples/algorithm/trees/CheckSubtree.java
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,14 @@ public void testSubtree() {
4949
*/
5050
that.left.right = new Node(10);
5151
assertFalse(root.isSubtree(that));
52+
53+
/**
54+
* 15
55+
* /
56+
* 17
57+
*/
58+
Node another = new Node(15);
59+
another.left = new Node(17);
60+
assertFalse(root.isSubtree(another));
5261
}
5362
}
Collapse file

‎src/main/java/com/eprogrammerz/examples/algorithm/trees/Node.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/eprogrammerz/examples/algorithm/trees/Node.java
+5-21Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ public boolean validate() {
7171
public boolean isSubtree(Node that) {
7272
// do bfs on this
7373
// if node found same as of that.root
74-
// then clear the queue of tree
75-
// start bfs for both, each queue poll should match
74+
// then traverse tree and see if left and right on both trees match
7675

77-
if (that == null) return false;
76+
if (that == null) return true;
7877

7978
Queue<Node> thisQueue = new LinkedList<>();
8079
thisQueue.add(this);
@@ -99,26 +98,11 @@ public boolean isSubtree(Node that) {
9998
}
10099

101100
private boolean isSubtree(Node t1, Node t2) {
101+
if (t1 == null && t2 == null) return true;
102102

103-
Queue<Node> t1Queue = new LinkedList<>();
104-
t1Queue.add(t1);
103+
if (t1 == null || t2 == null || t1.data != t2.data) return false;
105104

106-
Queue<Node> t2Queue = new LinkedList<>();
107-
t2Queue.add(t2);
108-
109-
while (!t1Queue.isEmpty() && !t2Queue.isEmpty()) {
110-
Node t1Node = t1Queue.poll();
111-
Node t2Node = t2Queue.poll();
112-
113-
if (t1Node.data != t2Node.data) return false;
114-
115-
if (t1Node.left != null) t1Queue.add(t1Node.left);
116-
if (t1Node.right != null) t1Queue.add(t1Node.right);
117-
118-
if (t2Node.left != null) t2Queue.add(t2Node.left);
119-
if (t2Node.right != null) t2Queue.add(t2Node.right);
120-
}
121-
return t1Queue.isEmpty() && t2Queue.isEmpty();
105+
return isSubtree(t1.left, t2.left) && isSubtree(t1.right, t2.right);
122106
}
123107

124108
@Override

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.