Depth-First Search pattern
Lowest Common Ancestor of a Binary Tree
Given the root of a binary tree and two nodes p and q, return their lowest common ancestor: the deepest node that has both p and q as descendants (a node may be a descendant of itself). Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 236
- Medium
- 1 approach
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
Depth-First SearchLeetCode 236
Medium
The approach
- 01 Recursive DFS (bubble up) O(n)O(h) space
What you will learn
- How to read Lowest Common Ancestor of a Binary Tree and spot the depth-first search pattern in it
- Recursive DFS (bubble up): O(n) time, O(h) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Depth-First Search
See Lowest Common Ancestor of a Binary Tree run, step by step
Sign in and this page opens on the animation, with all 18 Depth-First Search problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.