Skip to content

Commit cbd1d60

Browse files
committed
ajust a few commits
1 parent 144f552 commit cbd1d60

File tree

3 files changed

+158
-161
lines changed

3 files changed

+158
-161
lines changed

src/main/java/joshua/leetcode/binarytree/LowestCommonAncestor.java

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
package joshua.leetcode.binarytree;
22

33
/**
4-
*Lowest Common Ancestor of a Binary Tree
4+
* Lowest Common Ancestor of a Binary Tree
55
*
66
* @see <a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/">leetcode link</a>
77
*/
88
public abstract class LowestCommonAncestor {
99

1010
/**
1111
* 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+
*
2324
* @param root
2425
* @param p
2526
* @param q
@@ -31,29 +32,27 @@ For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another exa
3132
* recursive way.
3233
* the inefficient factor of this way is that, even the true Lowest Common ancestor is found at root's left side,
3334
* still need to traverse root's right side.
34-
*
3535
*/
36-
static class Solution1 extends LowestCommonAncestor{
36+
static class Solution1 extends LowestCommonAncestor {
3737

3838
@Override
3939
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
40-
if(root==null)
40+
if (root == null)
4141
return null;
4242
/*if root is p or q, return directly.it covers the cases when p is parent of q or the other way, or this root only contains q or p.*/
43-
if(root.val==p.val||root.val==q.val)
43+
if (root.val == p.val || root.val == q.val)
4444
return root;
45-
TreeNode left=root.left!=null?lowestCommonAncestor(root.left,p,q):null;
46-
TreeNode right=root.right!=null?lowestCommonAncestor(root.right,p,q):null;
45+
TreeNode left = root.left != null ? lowestCommonAncestor(root.left, p, q) : null;
46+
TreeNode right = root.right != null ? lowestCommonAncestor(root.right, p, q) : null;
4747
/**
4848
* if left and right aren't null, then p and q is found respectively(which side doesn't matter)
4949
* then root is the lowest common ancestor.
5050
*/
51-
if(left!=null&&right!=null)
51+
if (left != null && right != null)
5252
return root;
5353
/*otherwise, q and p is found at left or right side of root, just return the result from lower level.*/
54-
if(left!=null||right!=null)
55-
{
56-
return left!=null?left:right;
54+
if (left != null || right != null) {
55+
return left != null ? left : right;
5756
}
5857
/*either p or q is root or under root. return null.*/
5958
return null;
@@ -64,46 +63,45 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
6463
* 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.
6564
* just return directly.
6665
*/
67-
static class Solution2 extends LowestCommonAncestor{
66+
static class Solution2 extends LowestCommonAncestor {
6867

6968
@Override
7069
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
71-
if(root==null)
70+
if (root == null)
7271
return null;
73-
LCAPair result= findLowestCommonAncestor(root,p,q);
74-
return result!=null?result.lca:null;
72+
LCAPair result = findLowestCommonAncestor(root, p, q);
73+
return result != null ? result.lca : null;
7574
}
7675

7776
private LCAPair findLowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
78-
if(root==null)
77+
if (root == null)
7978
return null;
8079
/*if root is p or q, return directly.it covers the cases when p is parent of q or the other way, or this root only contains q or p.*/
81-
if(root.val==p.val||root.val==q.val)
82-
return new LCAPair(root,false);
83-
LCAPair left= (root.left != null) ? findLowestCommonAncestor(root.left, p, q) : null;
84-
if(left!=null&&left.found)
80+
if (root.val == p.val || root.val == q.val)
81+
return new LCAPair(root, false);
82+
LCAPair left = (root.left != null) ? findLowestCommonAncestor(root.left, p, q) : null;
83+
if (left != null && left.found)
8584
return left;
86-
LCAPair right= (root.right != null) ? findLowestCommonAncestor(root.right, p, q) : null;
87-
if(right!=null&&left.found)
85+
LCAPair right = (root.right != null) ? findLowestCommonAncestor(root.right, p, q) : null;
86+
if (right != null && left.found)
8887
return right;
8988
/**
9089
* if left and right aren't null, then p and q is found respectively(which side doesn't matter)
9190
* then root is the lowest common ancestor.
9291
*/
93-
if(left!=null&&right!=null)
94-
return new LCAPair(root,true);
92+
if (left != null && right != null)
93+
return new LCAPair(root, true);
9594
/*otherwise, q and p is found at left or right side of root, just return the result from lower level.*/
96-
if(left!=null||right!=null)
97-
{
98-
return left!=null?left:right;
95+
if (left != null || right != null) {
96+
return left != null ? left : right;
9997
}
100-
/*either p or q is root or under root. return null.*/
98+
/*neither p or q is root or under root. return null.*/
10199
return null;
102100
}
103101

104102
class LCAPair {
105103
TreeNode lca;
106-
boolean found;
104+
boolean found;
107105

108106
public LCAPair(TreeNode lca, boolean found) {
109107
this.lca = lca;

src/main/java/joshua/leetcode/binarytree/bst/LowestCommonAncestor.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
import joshua.leetcode.binarytree.TreeNode;
44

55
/**
6-
*235 Lowest Common Ancestor of a Binary Search Tree
6+
* 235 Lowest Common Ancestor of a Binary Search Tree
7+
*
78
* @see <a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/">leetcode link</a>
89
*/
910
public abstract class LowestCommonAncestor {
1011

1112
/**
12-
*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.
2626
*
2727
* @param root
2828
* @param p
@@ -31,26 +31,27 @@ For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6.
3131
*/
3232
public abstract TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q);
3333

34-
static class Solution1 extends LowestCommonAncestor{
34+
static class Solution1 extends LowestCommonAncestor {
3535

3636
/**
3737
* this is a binary search tree. so compare the values of two nodes with root,
3838
* if both are smaller than the root, repeat comparison with root's left;
3939
* if both are bigger than the root, repeat comparison with root's right;
4040
* otherwise, return the root as the LCA;
41+
*
4142
* @param root
4243
* @param p
4344
* @param q
4445
* @return
4546
*/
4647
@Override
4748
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
48-
if(root==null)
49+
if (root == null)
4950
return null;
50-
if(p.val<root.val&&q.val<root.val)
51-
return lowestCommonAncestor(root.left,p,q);
52-
if(p.val>root.val&&q.val>root.val)
53-
return lowestCommonAncestor(root.right,p,q);
51+
if (p.val < root.val && q.val < root.val)
52+
return lowestCommonAncestor(root.left, p, q);
53+
if (p.val > root.val && q.val > root.val)
54+
return lowestCommonAncestor(root.right, p, q);
5455
return root;
5556
}
5657
}

0 commit comments

Comments
 (0)