28- Lowest common ancestor
Description
Given a binary tree root and two integers num1 and num2, create a function that returns the lowest common ancestor of num1 and num2. The lowest common ancestor is the deepest node in root that has both num1 and num2 as descendants, and we consider a node as a descendant of itself.
Note that all values are unique and that num1 and num2 always exist in the tree.
Example 1:
- Input: root = [4, 10, 6, 5, null, 7, 13, 2, 18, 1, null, 8, 16, 14, 9, null, null, null, null, null, null, null, null, null, null, 22, 3], num1 = 7, num2 = 16
- Output: 6
- Explanation:
Example 2:
- Input: root = [4, 10, 6, 5, null, 7, 13, 2, 18, 1, null, 8, 16, 14, 9, null, null, null, null, null, null, null, null, null, null, 22, 3], num1 = 2, num2 = 13
- Output: 4
- Explanation:
Example 3:
- Input: root = [4, 10, 6, 5, null, 7, 13, 2, 18, 1, null, 8, 16, 14, 9, null, null, null, null, null, null, null, null, null, null, 22, 3], num1 = 9, num2 = 18
- Output: 5
- Explanation:
Try some input:
Try to solve:
Select a language:
Solution:
Code:
Select a language:
Python code:
By getting paths:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
By checking if num1 is on the left and num2 is on the right or the opposite:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
Java code:
By getting paths:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
By checking if num1 is on the left and num2 is on the right or the opposite:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
C++ code:
By getting paths:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
By checking if num1 is on the left and num2 is on the right or the opposite:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
JavaScript code:
By getting paths:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
By checking if num1 is on the left and num2 is on the right or the opposite:
In case the code doesn't appear: click here
Time complexity: O(n)
Space complexity: O(h)
Frequently asked questions:
No questions yet.
You can ask a question in comment section below.
0 comments