Problem
You get the roots root and sub_root of two binary trees. In a binary tree, each node has at most two children: a left child and a right child.
A subtree of a tree is one node of the tree and all the nodes below that node. The whole tree is also a subtree of itself.
Return true if root has a subtree with the same shape and the same node values as sub_root. Otherwise, return false.
The examples show each tree as a list in level order. This list gives the nodes row by row, from left to right. The value null marks a missing child.
Example 1
- Input:
- root = [8,4,10,2,6], sub_root = [4,2,6]
- Output:
- true
- Explanation:
- The subtree that starts at node
4has the same shape and values assub_root.
Example 2
- Input:
- root = [8,4,10,2,6,null,null,null,3], sub_root = [4,2,6]
- Output:
- false
- Explanation:
- The subtree at node
4also has the node3below node2. So it is not the same assub_root.
Example 3
- Input:
- root = [1,1], sub_root = [1]
- Output:
- true
- Explanation:
- The subtree at the lower node
1is the same assub_root.
Constraints
- 1 <= number of nodes in root <= 2000
- 1 <= number of nodes in sub_root <= 1000
- -10^4 <= Node.val <= 10^4
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Height of Binary Tree After Subtree Removal Queries
Coding
- K-th Largest Perfect Subtree Size in Binary Tree
Coding
- Step-By-Step Directions From a Binary Tree Node to Another
Coding · Binary Lifting · Binary Tree · Depth-First Search
- Subtree Removal Game with Fibonacci Tree
Coding
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 572. Subtree of Another Tree
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source