Problem
You get the roots p and q of two binary trees. In a binary tree, each node has at most two children: a left child and a right child.
Return true if the two trees are the same. Otherwise, return false. Two trees are the same when they have the same shape. Also, the nodes at the same position must have the same value.
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:
- p = [4,2,6], q = [4,2,6]
- Output:
- true
Example 2
- Input:
- p = [7,3], q = [7,null,3]
- Output:
- false
- Explanation:
- In
p, the node3is the left child of7. Inq, the node3is the right child of7.
Example 3
- Input:
- p = [5,1,9], q = [5,9,1]
- Output:
- false
- Explanation:
- The two trees have the same shape. But the children of
5have different values.
Constraints
- 0 <= number of nodes in each tree <= 100
- -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.
- Number of Nodes in the Sub-Tree With the Same Label
Coding
- Alert Using Same Key-Card Three or More Times in a One Hour Period
Coding
- All Nodes Distance K in Binary Tree
Coding · Binary Tree · Breadth-First Search · Depth-First Search
- Amount of Time for Binary Tree to Be Infected
Coding · Binary Tree · Breadth-First Search · Depth-First Search
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 100. Same Tree
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source