236. Lowest Common Ancestor of a Binary Tree
Practice Lowest Common Ancestor of a Binary Tree for free on NudgeCode: Medium, Binary Tree pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Given a binary tree and two of its nodes p and q, return their lowest common ancestor: the deepest node that has both p and q as descendants (a node counts as a descendant of itself).
In the tests p and q are given by value; all values are unique. The expected output is the value of the node you return.
Example
root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 -> 3
Constraints
- 2 <= number of nodes <= 10^5
pandqexist in the tree