Skip to content

Commit 8aa6828

Browse files
authored
feat: update solutions to lc problems: No.0700,0701 (doocs#3329)
1 parent 6654235 commit 8aa6828

File tree

14 files changed

+240
-130
lines changed

14 files changed

+240
-130
lines changed

solution/0700-0799/0700.Search in a Binary Search Tree/README.md

+49-16
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### 方法一
60+
### 方法一:递归
61+
62+
我们判断当前节点是否为空或者当前节点的值是否等于目标值,如果是则返回当前节点。
63+
64+
否则,如果当前节点的值大于目标值,则递归搜索左子树,否则递归搜索右子树。
65+
66+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
6167

6268
<!-- tabs:start -->
6369

@@ -71,13 +77,13 @@ tags:
7177
# self.left = left
7278
# self.right = right
7379
class Solution:
74-
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
80+
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
7581
if root is None or root.val == val:
7682
return root
7783
return (
78-
self.searchBST(root.right, val)
79-
if root.val < val
80-
else self.searchBST(root.left, val)
84+
self.searchBST(root.left, val)
85+
if root.val > val
86+
else self.searchBST(root.right, val)
8187
)
8288
```
8389

@@ -104,7 +110,7 @@ class Solution {
104110
if (root == null || root.val == val) {
105111
return root;
106112
}
107-
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
113+
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
108114
}
109115
}
110116
```
@@ -126,8 +132,10 @@ class Solution {
126132
class Solution {
127133
public:
128134
TreeNode* searchBST(TreeNode* root, int val) {
129-
if (!root || root->val == val) return root;
130-
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
135+
if (!root || root->val == val) {
136+
return root;
137+
}
138+
return root->val > val ? searchBST(root->left, val) : searchBST(root->right, val);
131139
}
132140
};
133141
```
@@ -143,14 +151,39 @@ public:
143151
* Right *TreeNode
144152
* }
145153
*/
146-
func searchBST(root *TreeNode, val int) *TreeNode {
147-
if root == nil || root.Val == val {
148-
return root
149-
}
150-
if root.Val < val {
151-
return searchBST(root.Right, val)
152-
}
153-
return searchBST(root.Left, val)
154+
func searchBST(root *TreeNode, val int) *TreeNode {
155+
if root == nil || root.Val == val {
156+
return root
157+
}
158+
if root.Val > val {
159+
return searchBST(root.Left, val)
160+
}
161+
return searchBST(root.Right, val)
162+
}
163+
```
164+
165+
#### TypeScript
166+
167+
```ts
168+
/**
169+
* Definition for a binary tree node.
170+
* class TreeNode {
171+
* val: number
172+
* left: TreeNode | null
173+
* right: TreeNode | null
174+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
175+
* this.val = (val===undefined ? 0 : val)
176+
* this.left = (left===undefined ? null : left)
177+
* this.right = (right===undefined ? null : right)
178+
* }
179+
* }
180+
*/
181+
182+
function searchBST(root: TreeNode | null, val: number): TreeNode | null {
183+
if (root === null || root.val === val) {
184+
return root;
185+
}
186+
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
154187
}
155188
```
156189

solution/0700-0799/0700.Search in a Binary Search Tree/README_EN.md

+49-16
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
### Solution 1: Recursion
57+
58+
We check if the current node is null or if the current node's value equals the target value. If so, we return the current node.
59+
60+
Otherwise, if the current node's value is greater than the target value, we recursively search the left subtree; otherwise, we recursively search the right subtree.
61+
62+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
5763

5864
<!-- tabs:start -->
5965

@@ -67,13 +73,13 @@ tags:
6773
# self.left = left
6874
# self.right = right
6975
class Solution:
70-
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
76+
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
7177
if root is None or root.val == val:
7278
return root
7379
return (
74-
self.searchBST(root.right, val)
75-
if root.val < val
76-
else self.searchBST(root.left, val)
80+
self.searchBST(root.left, val)
81+
if root.val > val
82+
else self.searchBST(root.right, val)
7783
)
7884
```
7985

@@ -100,7 +106,7 @@ class Solution {
100106
if (root == null || root.val == val) {
101107
return root;
102108
}
103-
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
109+
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
104110
}
105111
}
106112
```
@@ -122,8 +128,10 @@ class Solution {
122128
class Solution {
123129
public:
124130
TreeNode* searchBST(TreeNode* root, int val) {
125-
if (!root || root->val == val) return root;
126-
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
131+
if (!root || root->val == val) {
132+
return root;
133+
}
134+
return root->val > val ? searchBST(root->left, val) : searchBST(root->right, val);
127135
}
128136
};
129137
```
@@ -139,14 +147,39 @@ public:
139147
* Right *TreeNode
140148
* }
141149
*/
142-
func searchBST(root *TreeNode, val int) *TreeNode {
143-
if root == nil || root.Val == val {
144-
return root
145-
}
146-
if root.Val < val {
147-
return searchBST(root.Right, val)
148-
}
149-
return searchBST(root.Left, val)
150+
func searchBST(root *TreeNode, val int) *TreeNode {
151+
if root == nil || root.Val == val {
152+
return root
153+
}
154+
if root.Val > val {
155+
return searchBST(root.Left, val)
156+
}
157+
return searchBST(root.Right, val)
158+
}
159+
```
160+
161+
#### TypeScript
162+
163+
```ts
164+
/**
165+
* Definition for a binary tree node.
166+
* class TreeNode {
167+
* val: number
168+
* left: TreeNode | null
169+
* right: TreeNode | null
170+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
171+
* this.val = (val===undefined ? 0 : val)
172+
* this.left = (left===undefined ? null : left)
173+
* this.right = (right===undefined ? null : right)
174+
* }
175+
* }
176+
*/
177+
178+
function searchBST(root: TreeNode | null, val: number): TreeNode | null {
179+
if (root === null || root.val === val) {
180+
return root;
181+
}
182+
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
150183
}
151184
```
152185

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
class Solution {
1313
public:
1414
TreeNode* searchBST(TreeNode* root, int val) {
15-
if (!root || root->val == val) return root;
16-
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
15+
if (!root || root->val == val) {
16+
return root;
17+
}
18+
return root->val > val ? searchBST(root->left, val) : searchBST(root->right, val);
1719
}
1820
};

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* Right *TreeNode
77
* }
88
*/
9-
func searchBST(root *TreeNode, val int) *TreeNode {
10-
if root == nil || root.Val == val {
11-
return root
12-
}
13-
if root.Val < val {
14-
return searchBST(root.Right, val)
15-
}
16-
return searchBST(root.Left, val)
9+
func searchBST(root *TreeNode, val int) *TreeNode {
10+
if root == nil || root.Val == val {
11+
return root
12+
}
13+
if root.Val > val {
14+
return searchBST(root.Left, val)
15+
}
16+
return searchBST(root.Right, val)
1717
}

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public TreeNode searchBST(TreeNode root, int val) {
1818
if (root == null || root.val == val) {
1919
return root;
2020
}
21-
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
21+
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
2222
}
2323
}

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
8+
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
99
if root is None or root.val == val:
1010
return root
1111
return (
12-
self.searchBST(root.right, val)
13-
if root.val < val
14-
else self.searchBST(root.left, val)
12+
self.searchBST(root.left, val)
13+
if root.val > val
14+
else self.searchBST(root.right, val)
1515
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function searchBST(root: TreeNode | null, val: number): TreeNode | null {
16+
if (root === null || root.val === val) {
17+
return root;
18+
}
19+
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
20+
}

0 commit comments

Comments
 (0)