↓Skip to main content
  1. LeetCode/

LeetCode 236: Lowest Common Ancestor of a Binary Tree

·2 mins· ·
LeetCode Medium Tree Depth-First-Search Binary-Tree Binary-Lifting
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: medium First Attempt: 2022-11-18

  • Submissions: Runtime Error → Wrong Answer → Accepted ×6

Approach
#

A general binary tree has no ordering, so use return values (Tree Template 4):

  • Reaching None, p or q returns itself.
  • Both subtrees return something: p and q split here, so this node is the LCA.
  • Only one side returns something: pass it up.

My Attempts
#

The first two versions reused LC 235’s BST approach, choosing left or right by value, but a general binary tree has no ordering. In [8, 45, 61] with p = 61 and q = 45 the answer is the root 8; since 8 is smaller than both, it went right. Switching to recursive return values passed.

Solution
#

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if not root or root is p or root is q:
            return root
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)
        if left and right:
            return root
        return left or right

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • Return immediately at p or q: if the other lies below, this node is the LCA anyway.

Interview Explanation
#

Recursively, None, p or q returns itself. If both sides return something they split here and this node is the LCA; otherwise pass up the non-empty side. Time O(n).

Related

LeetCode 113: Path Sum II
·2 mins
LeetCode Medium Backtracking Tree Depth-First-Search Binary-Tree
LeetCode 113 note: backtrack with the current path and store a copy at a leaf whose total matches; values can be negative, so no early pruning
LeetCode 129: Sum Root to Leaf Numbers
·1 min
LeetCode Medium Tree Depth-First-Search Binary-Tree
LeetCode 129 note: on the way down multiply the current number by 10 and add the node’s value, adding it to the total at a leaf
LeetCode 337: House Robber III
·2 mins
LeetCode Medium Dynamic-Programming Tree Depth-First-Search Binary-Tree
LeetCode 337 note: tree DP where each node returns the best (rob it, skip it) pair; not robbing alternate levels
LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum
LeetCode 199: Binary Tree Right Side View
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 199 note: in a level-order traversal, each level’s rightmost node is what you see from the right
LeetCode 230: Kth Smallest Element in a BST
·1 min
LeetCode Medium Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 230 note: a BST’s in-order traversal is ascending, so stop an iterative in-order walk at the k-th node