#872

Leaf-Similar Trees

newbie · 275 · lc easy +19 · verified · 70.2% accepted · 4,370 likes · top 79%

Description

The leaf value sequence of a binary tree is formed by reading all leaf values from left to right.

For example, a tree may produce the leaf sequence (6, 7, 4, 9, 8).

Two binary trees are leaf-similar if their leaf value sequences are identical.

Return true if and only if the trees with roots root1 and root2 are leaf-similar.

Example 1:

Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
Output: true

Example 2:

Input: root1 = [1,2,3], root2 = [1,3,2]
Output: false

Code

1
2
3
4
5
6
7
8
9