Leaf-Similar Trees

Consider all the leaves of a binary tree in left-to-right order; the values of those leaves form a leaf value sequence. Two binary trees are leaf-similar if their leaf value sequences are the same. Determine if two binary trees with given head nodes are leaf-similar.

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].
Saving… · 9 lines
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right

class Solution:
def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool:
Console

Click Run to execute sample tests.