2dbi
Home/Meta/Lowest Common Ancestor of a Binary Tree III
MMeta·DSAE4Onsite – Coding 1

Lowest Common Ancestor of a Binary Tree III

Problem

Each node has a parent pointer. Given two nodes p and q, return their lowest common ancestor.

Example

Like finding the intersection of two linked lists going up to the root.

Constraints

  • Nodes are guaranteed to exist in the tree

Approach

Two-pointer technique walking up via parent pointers and switching to the other node's root — O(1) space.

added 6 days ago
LeadersAccount