Subtree of Another Tree
Easy · Trees
Jump to codeDetermine whether a nonempty binary tree subRoot appears as an entire subtree of root, with exactly the same node values and null-child structure.
Examples
Example 1
root = [3,4,5,1,2] subRoot = [4,1,2]
Output: true
Example 2
root = [3,4,5,1,2,null,null,null,null,0] subRoot = [4,1,2]
Output: false
Constraints
- 1 <= The number of nodes in both trees <= 100
- -100 <= root.val, subRoot.val <= 100
Function contract
Trees use level-order arrays, with null for a missing child. Your code receives TreeNode objects with val, left, and right fields.
Need a hint?
At each candidate root, use the same-tree comparison.
Real tree interview questions
Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.
- Lowest Common Manager of a Group of Employees in an Org ChartAmazon
- Repeatedly Delete Leaves Matching a Target Value From a Binary TreeGoogle
- Merge two N-ary trees by key rulesLinkedIn
Solution approach
Traverse root. At each node, test whether its whole subtree equals subRoot using value and structure comparison. Return true on any match. If a candidate fails, continue with its children. An empty root cannot contain a nonempty subRoot.
Complexity
O(nm) worst-case time and O(h1 + h2) stack space for the straightforward recursive method.
Python reference solution
def isSubtree(root, subRoot):
def same(a, b):
if a is None or b is None:
return a is b
return a.val == b.val and same(a.left, b.left) and same(a.right, b.right)
if subRoot is None:
return True
if root is None:
return False
return same(root, subRoot) or isSubtree(root.left, subRoot) or isSubtree(root.right, subRoot)
JavaScript solution
function isSubtree(root, subRoot) {
function same(a, b) {
if (!a || !b) return a === b;
return a.val === b.val && same(a.left, b.left) && same(a.right, b.right);
}
if (!subRoot) return true;
if (!root) return false;
return same(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
Java solution
import java.util.*;
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (subRoot == null) return true;
if (root == null) return false;
return same(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
private boolean same(TreeNode a, TreeNode b) {
if (a == null || b == null) return a == b;
return a.val == b.val && same(a.left, b.left) && same(a.right, b.right);
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if (!subRoot) return true;
if (!root) return false;
return same(root, subRoot) || isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
bool same(TreeNode* a, TreeNode* b) {
if (!a || !b) return a == b;
return a->val == b->val && same(a->left, b->left) && same(a->right, b->right);
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 7 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
3,
4,
5,
1,
2
],
[
4,
1,
2
]
]true
[
[
3,
4,
5,
1,
2,
null,
null,
null,
null,
0
],
[
4,
1,
2
]
]false
[
[
1
],
[
1
]
]true
[
[
1,
2,
3
],
[
2
]
]true
[
[
1,
2,
3
],
[
4
]
]false
Drafts are saved per language in this browser when storage is available.