↓Skip to main content
  1. LeetCode/

LeetCode 872: Leaf-Similar Trees

·1 min· ·
LeetCode Easy Tree Depth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-17

  • Submissions: Wrong Answer ×2 → Accepted ×7 → Runtime Error → Accepted

Approach
#

The leaf sequence is in left-to-right order, which is exactly the order a DFS (left before right) finds leaves (Tree Template 1). Collect for both trees and compare.

My Attempts
#

The first two versions collected leaves with a BFS queue, which orders them level by level. For [4, None, 3, 2, 2, 8, None, 1] and [3, 5, None, 4, 8, None, 1], the left-to-right leaves are [8, 1] and [1, 8], so the answer is False; BFS collected [8, 1] for both and it returned True.

Solution
#

class Solution:
    def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
        def leaves(node):
            if not node:
                return []
            if not node.left and not node.right:
                return [node.val]
            return leaves(node.left) + leaves(node.right)
        return leaves(root1) == leaves(root2)

Complexity
#

Time O(n + m), space O(n + m).

Mistakes to Avoid
#

  • With BFS, a shallower leaf comes before a deeper leaf further left, which is the wrong order.

Interview Explanation
#

I collect each tree’s leaves with a left-first DFS, which yields left-to-right order, and compare the sequences. Time O(n + m).

Related

LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count
LeetCode 671: Second Minimum Node In a Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 671 note: the root is the minimum; the second minimum is the smallest value larger than the root, and only subtrees equal to the root need searching
LeetCode 563: Binary Tree Tilt
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 563 note: return subtree sums in post-order; each node’s tilt is the difference of its subtree sums, added to the answer
LeetCode 572: Subtree of Another Tree
·2 mins
LeetCode Easy Tree Depth-First-Search String-Matching Binary-Tree
LeetCode 572 note: for every node of root, check whether its subtree equals subRoot; recurse with isSubtree, not by comparing only the children
LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 543: Diameter of Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 543 note: the diameter may skip the root; return heights in post-order while updating the answer with left + right height at every node, counting edges