Skip to content

Commit 3e925c3

Browse files
committed
feat: update solutions to leetcode problem: No.0236
1 parent 94c4149 commit 3e925c3

File tree

4 files changed

+16
-37
lines changed

4 files changed

+16
-37
lines changed

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README.md

+4-11
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,12 @@ class Solution:
110110

111111
class Solution {
112112
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
113-
if (p == q) {
114-
return p;
115-
}
116113
while (root != null) {
117-
if (root.val < p.val && root.val < q.val) {
118-
root = root.right;
119-
} else if (root.val > p.val && root.val > q.val) {
120-
root = root.left;
121-
} else {
122-
return root;
123-
}
114+
if (root.val < p.val && root.val < q.val) root = root.right;
115+
else if (root.val > p.val && root.val > q.val) root = root.left;
116+
else return root;
124117
}
125-
return null;
118+
return root;
126119
}
127120
}
128121
```

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README_EN.md

+6-13
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ Iterative:
6363

6464
class Solution:
6565
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
66-
if p == q:
67-
return p
66+
if root is None:
67+
return None
6868
while root:
6969
if root.val < p.val and root.val < q.val:
7070
root = root.right
@@ -112,19 +112,12 @@ Iterative:
112112

113113
class Solution {
114114
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
115-
if (p == q) {
116-
return p;
117-
}
118115
while (root != null) {
119-
if (root.val < p.val && root.val < q.val) {
120-
root = root.right;
121-
} else if (root.val > p.val && root.val > q.val) {
122-
root = root.left;
123-
} else {
124-
return root;
125-
}
116+
if (root.val < p.val && root.val < q.val) root = root.right;
117+
else if (root.val > p.val && root.val > q.val) root = root.left;
118+
else return root;
126119
}
127-
return null;
120+
return root;
128121
}
129122
}
130123
```

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@
1010

1111
class Solution {
1212
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13-
if (p == q) {
14-
return p;
15-
}
1613
while (root != null) {
17-
if (root.val < p.val && root.val < q.val) {
18-
root = root.right;
19-
} else if (root.val > p.val && root.val > q.val) {
20-
root = root.left;
21-
} else {
22-
return root;
23-
}
14+
if (root.val < p.val && root.val < q.val) root = root.right;
15+
else if (root.val > p.val && root.val > q.val) root = root.left;
16+
else return root;
2417
}
25-
return null;
18+
return root;
2619
}
2720
}

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class Solution:
99
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
10-
if p == q:
11-
return p
10+
if root is None:
11+
return None
1212
while root:
1313
if root.val < p.val and root.val < q.val:
1414
root = root.right

0 commit comments

Comments
 (0)