Problem
You get the roots of two binary trees, root1 and root2. The input shows each tree as a list in level order. This order lists the nodes one level at a time, from left to right. A null value marks a missing child.
A leaf is a node with no children. The leaf sequence of a tree is the list of its leaf values, from left to right.
Two trees are leaf-similar when they have the same leaf sequence. Return true if root1 and root2 are leaf-similar. Otherwise, return false.
Example 1
- Input:
- root1 = [5,3,8,1,4,null,9], root2 = [2,1,7,null,null,4,9]
- Output:
- true
- Explanation:
- Both trees have the leaf sequence
[1, 4, 9]. The shapes of the trees are different, but only the leaves count.
Example 2
- Input:
- root1 = [1,2,3], root2 = [1,3,2]
- Output:
- false
- Explanation:
- The leaf sequence of
root1is[2, 3]. The leaf sequence ofroot2is[3, 2].
Example 3
- Input:
- root1 = [6], root2 = [8,6]
- Output:
- true
- Explanation:
- In
root1, the root has no children, so it is a leaf.
Constraints
- 1 <= number of nodes in each tree <= 200
- 0 <= Node.val <= 200
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
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 · 2 records
Google
Leetcode 872. Leaf-Similar Trees
Reported Aug 24, 2026 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceSnowflake
Leetcode 872. Leaf-Similar Trees
Reported Mar 17, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source