You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
6
+
7
+
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).”
8
+
9
+
Example 1:
10
+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
11
+
Output: 3
12
+
Explanation: The LCA of nodes 5 and 1 is 3.
13
+
14
+
Example 2:
15
+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
16
+
Output: 5
17
+
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
18
+
19
+
Example 3:
20
+
Input: root = [1,2], p = 1, q = 2
21
+
Output: 1
22
+
23
+
Constraints:
24
+
The number of nodes in the tree is in the range [2, 105].
| 28. |[Remove All Adjacent Duplicates In String](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/606/week-4-june-22nd-june-28th/3794/)[cpp](./28.%20Remove%20All%20Adjacent%20Duplicates%20In%20String.cpp)|
| 30. |[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/607/week-5-june-29th-june-30th/3797/)[cpp](./30.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree.cpp)|
0 commit comments