Lowest Common Ancestor of a Binary Tree
Find the lowest common ancestor (LCA) of two nodes in a binary tree. The LCA is the lowest node that has both p and q as descendants.
Lowest Common Ancestor of a Binary Tree

Find the lowest common ancestor (LCA) of two nodes in a binary tree. The LCA is the lowest node that has both p and q as descendants.

Example 1:
Input: {"root":[3,5,1,6,2,0,8,null,null,7,4],"p":5,"q":1}
Output: 3
Input
arr ={"root":[3,5,1,6,2,0,8,null,null,7,4],"p":5,"q":1}

Finding LCA of nodes with values 5 and 1

Target (p/q)
Current Node
LCA Found
Visited
p
5
q
1
DFS Path

-

3

p

5

q

1

6

2

0

8

7

4

Variables
No variables to display
DepthFunction Call
Stack empty
0/13