Skip to content

feat: update solutions to lc problems: No.0700,0701 #3329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 49 additions & 16 deletions solution/0700-0799/0700.Search in a Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:递归

我们判断当前节点是否为空或者当前节点的值是否等于目标值,如果是则返回当前节点。

否则,如果当前节点的值大于目标值,则递归搜索左子树,否则递归搜索右子树。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。

<!-- tabs:start -->

Expand All @@ -71,13 +77,13 @@ tags:
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if root is None or root.val == val:
return root
return (
self.searchBST(root.right, val)
if root.val < val
else self.searchBST(root.left, val)
self.searchBST(root.left, val)
if root.val > val
else self.searchBST(root.right, val)
)
```

Expand All @@ -104,7 +110,7 @@ class Solution {
if (root == null || root.val == val) {
return root;
}
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
}
```
Expand All @@ -126,8 +132,10 @@ class Solution {
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (!root || root->val == val) return root;
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
if (!root || root->val == val) {
return root;
}
return root->val > val ? searchBST(root->left, val) : searchBST(root->right, val);
}
};
```
Expand All @@ -143,14 +151,39 @@ public:
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val < val {
return searchBST(root.Right, val)
}
return searchBST(root.Left, val)
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val > val {
return searchBST(root.Left, val)
}
return searchBST(root.Right, val)
}
```

#### TypeScript

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function searchBST(root: TreeNode | null, val: number): TreeNode | null {
if (root === null || root.val === val) {
return root;
}
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
```

Expand Down
65 changes: 49 additions & 16 deletions solution/0700-0799/0700.Search in a Binary Search Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Recursion

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.

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.

<!-- tabs:start -->

Expand All @@ -67,13 +73,13 @@ tags:
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if root is None or root.val == val:
return root
return (
self.searchBST(root.right, val)
if root.val < val
else self.searchBST(root.left, val)
self.searchBST(root.left, val)
if root.val > val
else self.searchBST(root.right, val)
)
```

Expand All @@ -100,7 +106,7 @@ class Solution {
if (root == null || root.val == val) {
return root;
}
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
}
```
Expand All @@ -122,8 +128,10 @@ class Solution {
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (!root || root->val == val) return root;
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
if (!root || root->val == val) {
return root;
}
return root->val > val ? searchBST(root->left, val) : searchBST(root->right, val);
}
};
```
Expand All @@ -139,14 +147,39 @@ public:
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val < val {
return searchBST(root.Right, val)
}
return searchBST(root.Left, val)
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val > val {
return searchBST(root.Left, val)
}
return searchBST(root.Right, val)
}
```

#### TypeScript

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function searchBST(root: TreeNode | null, val: number): TreeNode | null {
if (root === null || root.val === val) {
return root;
}
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (!root || root->val == val) return root;
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
if (!root || root->val == val) {
return root;
}
return root->val > val ? searchBST(root->left, val) : searchBST(root->right, val);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val < val {
return searchBST(root.Right, val)
}
return searchBST(root.Left, val)
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val > val {
return searchBST(root.Left, val)
}
return searchBST(root.Right, val)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) {
return root;
}
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if root is None or root.val == val:
return root
return (
self.searchBST(root.right, val)
if root.val < val
else self.searchBST(root.left, val)
self.searchBST(root.left, val)
if root.val > val
else self.searchBST(root.right, val)
)
20 changes: 20 additions & 0 deletions solution/0700-0799/0700.Search in a Binary Search Tree/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function searchBST(root: TreeNode | null, val: number): TreeNode | null {
if (root === null || root.val === val) {
return root;
}
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
Loading
Loading