Lowest Common Ancestor Of A Binary Tree

medium
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.

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.

Notice

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.

Example

Input
10 5 3 13 null null -2 null null 2 null 1 null null -3 null 11 null null
13 3
Output
3
Previous
Path Sum Equal To Given Value
Next
Unique Binary Search Trees 2

Related Questions