Skip to main content
LeetCode 872, Easy. Topics: Tree, Depth-First Search, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a <strong>leaf value sequence</strong>.</p> <p>For example, in the given tree above, the leaf value sequence is <code>(6, 7, 4, 9, 8)</code>.</p> <p>Two binary trees are considered <em>leaf-similar</em> if their leaf value sequence is the same.</p> <p>Return <code>true</code> if and only if the two given trees with head nodes <code>root1</code> and <code>root2</code> are leaf-similar.

Examples

Example 1
Example 2

Constraints

  • The number of nodes in each tree will be in the range [1, 200].
  • Both of the given trees will have values in the range [0, 200].

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026