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.
12
-
13
-
According to the definition of LCA on Wikipedia:
14
-
“The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
15
-
_______3______
16
-
/ \
17
-
___5__ ___1__
18
-
/ \ / \
19
-
6 _2 0 8
20
-
/ \
21
-
7 4
22
-
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
12
+
* <p/>
13
+
* According to the definition of LCA on Wikipedia:
14
+
* “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
15
+
* _______3______
16
+
* / \
17
+
* ___5__ ___1__
18
+
* / \ / \
19
+
* 6 _2 0 8
20
+
* / \
21
+
* 7 4
22
+
* For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
23
+
*
23
24
* @param root
24
25
* @param p
25
26
* @param q
@@ -31,29 +32,27 @@ For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another exa
31
32
* recursive way.
32
33
* the inefficient factor of this way is that, even the true Lowest Common ancestor is found at root's left side,
* improvement of solution1: if both left and right are't null, that means this unique lowest common ancestor is found so no need to traverse the remaining nodes.
*Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
13
-
14
-
According to the definition of LCA on Wikipedia:
15
-
“The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
16
-
_______6______
17
-
/ \
18
-
___2__ ___8__
19
-
/ \ / \
20
-
0 _4 7 9
21
-
/ \
22
-
3 5
23
-
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6.
24
-
Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
25
-
*
13
+
* Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
14
+
* <p/>
15
+
* According to the definition of LCA on Wikipedia:
16
+
* “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
17
+
* _______6______
18
+
* / \
19
+
* ___2__ ___8__
20
+
* / \ / \
21
+
* 0 _4 7 9
22
+
* / \
23
+
* 3 5
24
+
* For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6.
25
+
* Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
26
26
*
27
27
* @param root
28
28
* @param p
@@ -31,26 +31,27 @@ For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6.
0 commit comments