Categorygithub.com/szhou12/leetcode-goleetcode1650-Lowest-Common-Ancestor-of-a-Binary-Tree-III
package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev

# README

LeetCode 1650. Lowest Common Ancestor of a Binary Tree III

Solution idea

LCA with parent pointer

Method 1: HashMap

  • Traverse from one of the nodes all the way up to the root

  • Save its all the ancestors in a hash map (including itself)

  • Then, traverse from the other node all the way up to the root, return the first parent node that is already in the hash map.

Time complexity = $O(h)$, Space complexity = $O(h)$

Method 2: NOT easy-to-understand, skipped

Resource

Solution - LeetCode 1650. Lowest Common Ancestor of a Binary Tree III