Algo-Sim › Binary Trees & BST
Subtree of Another Tree
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Try every node of root as the start of a copy of subRoot, comparing with Same Tree each time.
Problem
Given the roots of two binary trees root and subRoot, return true if root contains a subtree identical to subRoot: some node of root together with all of its descendants, matching in shape and values.
Constraints
- 1 ≤ nodes in root ≤ 2000
- 1 ≤ nodes in subRoot ≤ 1000
- -10⁴ ≤ Node.val ≤ 10⁴
Edge cases to test
- subRoot equals root
- Same values in a different shape
- subRoot matches only part of a larger subtree (must match to the leaves)
Follow-up questions
- Can it beat O(m·n)?
- Serialise both trees with null markers and run KMP (or hash every subtree): O(m + n).
- Why do null markers matter?
- Without them, different shapes can serialise to the same string.
Solution
Java
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null) return false;
if (isSameTree(root, subRoot)) return true;
return isSubtree(root.left, subRoot)
|| isSubtree(root.right, subRoot);
}
private boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}Python
def is_subtree(root, sub_root):
if not root:
return False
if is_same_tree(root, sub_root):
return True
return (is_subtree(root.left, sub_root)
or is_subtree(root.right, sub_root))
def is_same_tree(p, q):
if not p and not q:
return True
if not p or not q:
return False
if p.val != q.val:
return False
return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isSubtree(TreeNode *root, TreeNode *subRoot) {
if (root == nullptr)
return false;
if (isSameTree(root, subRoot))
return true;
return isSubtree(root->left, subRoot)
|| isSubtree(root->right, subRoot);
}
bool isSameTree(TreeNode *p, TreeNode *q) {
if (p == nullptr && q == nullptr)
return true;
if (p == nullptr || q == nullptr)
return false;
if (p->val != q->val)
return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)