Skip to content

Commit 6e5c7a7

Browse files
committed
feat: update solutions to leetcode problem: No.0235
1 parent 3e925c3 commit 6e5c7a7

File tree

4 files changed

+117
-47
lines changed

4 files changed

+117
-47
lines changed

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

+44-21
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959

6060
class Solution:
6161
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
62-
if p == q:
63-
return p
6462
while root:
6563
if root.val < p.val and root.val < q.val:
6664
root = root.right
@@ -145,33 +143,58 @@ class Solution {
145143

146144
### **Go**
147145

146+
迭代:
147+
148+
```go
149+
/**
150+
* Definition for a binary tree node.
151+
* type TreeNode struct {
152+
* Val int
153+
* Left *TreeNode
154+
* Right *TreeNode
155+
* }
156+
*/
157+
158+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
159+
for root != nil {
160+
// 如果 p、q 的值都小于 root,说明 p、q 肯定在 root 的左子树中;
161+
// 如果 p、q 都大于 root,说明肯定在 root 的右子树中;
162+
// 如果一个在左一个在右,则说明此时的 root 记为对应的最近公共祖先。
163+
if root.Val > p.Val && root.Val > q.Val {
164+
root = root.Left
165+
} else if root.Val < p.Val && root.Val < q.Val {
166+
root = root.Right
167+
} else {
168+
return root
169+
}
170+
}
171+
return nil
172+
}
173+
```
174+
175+
递归:
176+
148177
```go
149178
/**
150-
* Definition for TreeNode.
179+
* Definition for a binary tree node.
151180
* type TreeNode struct {
152-
* Val int
153-
* Left *ListNode
154-
* Right *ListNode
181+
* Val int
182+
* Left *TreeNode
183+
* Right *TreeNode
155184
* }
156185
*/
157-
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
186+
187+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
158188
if root == nil {
159-
return nil
189+
return root
160190
}
161-
162-
for root != nil {
163-
//如果p、q的值都小于root,说明p q 肯定在root的左子树中;
164-
//如果p q都大于root,说明肯定在root的右子树中
165-
//如果一个在左一个在右 则说明此时的root记为对应的最近公共祖先
166-
if root.Val > p.Val && root.Val > q.Val {
167-
root = root.Left
168-
} else if root.Val < p.Val && root.Val < q.Val {
169-
root = root.Right
170-
} else {
171-
return root
172-
}
191+
if root.Val < p.Val && root.Val < q.Val {
192+
return lowestCommonAncestor(root.Right, p, q)
193+
}
194+
if root.Val > p.Val && root.Val > q.Val {
195+
return lowestCommonAncestor(root.Left, p, q)
173196
}
174-
return nil
197+
return root
175198
}
176199
```
177200

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

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

6464
class Solution:
6565
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
66-
if root is None:
67-
return None
6866
while root:
6967
if root.val < p.val and root.val < q.val:
7068
root = root.right
@@ -145,6 +143,60 @@ class Solution {
145143
}
146144
```
147145

146+
### **Go**
147+
148+
Iterative:
149+
150+
```go
151+
/**
152+
* Definition for a binary tree node.
153+
* type TreeNode struct {
154+
* Val int
155+
* Left *TreeNode
156+
* Right *TreeNode
157+
* }
158+
*/
159+
160+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
161+
for root != nil {
162+
if root.Val > p.Val && root.Val > q.Val {
163+
root = root.Left
164+
} else if root.Val < p.Val && root.Val < q.Val {
165+
root = root.Right
166+
} else {
167+
return root
168+
}
169+
}
170+
return nil
171+
}
172+
```
173+
174+
Recursive:
175+
176+
```go
177+
/**
178+
* Definition for a binary tree node.
179+
* type TreeNode struct {
180+
* Val int
181+
* Left *TreeNode
182+
* Right *TreeNode
183+
* }
184+
*/
185+
186+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
187+
if root == nil {
188+
return root
189+
}
190+
if root.Val < p.Val && root.Val < q.Val {
191+
return lowestCommonAncestor(root.Right, p, q)
192+
}
193+
if root.Val > p.Val && root.Val > q.Val {
194+
return lowestCommonAncestor(root.Left, p, q)
195+
}
196+
return root
197+
}
198+
```
199+
148200
### **...**
149201

150202
```
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
/**
2-
* Definition for TreeNode.
2+
* Definition for a binary tree node.
33
* type TreeNode struct {
4-
* Val int
5-
* Left *ListNode
6-
* Right *ListNode
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
77
* }
88
*/
9-
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
10-
if root == nil {
11-
return nil
12-
}
139

14-
for root != nil {
15-
//如果p、q的值都小于root,说明p q 肯定在root的左子树中;
16-
//如果p q都大于root,说明肯定在root的右子树中
17-
//如果一个在左一个在右 则说明此时的root记为对应的最近公共祖先
18-
if root.Val > p.Val && root.Val > q.Val {
19-
root = root.Left
20-
} else if root.Val < p.Val && root.Val < q.Val {
21-
root = root.Right
22-
} else {
23-
return root
24-
}
25-
}
26-
return nil
27-
}
10+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
11+
for root != nil {
12+
// 如果 p、q 的值都小于 root,说明 p、q 肯定在 root 的左子树中;
13+
// 如果 p、q 都大于 root,说明肯定在 root 的右子树中;
14+
// 如果一个在左一个在右,则说明此时的 root 记为对应的最近公共祖先。
15+
if root.Val > p.Val && root.Val > q.Val {
16+
root = root.Left
17+
} else if root.Val < p.Val && root.Val < q.Val {
18+
root = root.Right
19+
} else {
20+
return root
21+
}
22+
}
23+
return nil
24+
}

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

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

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

0 commit comments

Comments
 (0)