Redirecting to
NADOS

Lowest Common Ancestor Of A Binary Tree

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

1. Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
2. According to the definition of LCA on Wikipedia:
The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has
both p and q as descendants (where we allow a node to be a descendant of itself).
3. If LCA does not exist in the tree print null.
Input Format
Input is managed for you.
Output Format
Output is managed for you. 
Question Video
Constraints
1. The number of nodes in the tree is in the range [-1000, 1000].
2. -109 <= Node.val <= 109
All Node.val are unique.
p != q
p and q may or may not exist in the tree.
Sample Input
10 5 3 13 null null -2 null null 2 null 1 null null -3 null 11 null null
13 3
Sample Output
3


  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name