diff --git a/solution/0700-0799/0700.Search in a Binary Search Tree/README.md b/solution/0700-0799/0700.Search in a Binary Search Tree/README.md index 958511ec505d5..13521493f3c97 100644 --- a/solution/0700-0799/0700.Search in a Binary Search Tree/README.md +++ b/solution/0700-0799/0700.Search in a Binary Search Tree/README.md @@ -57,7 +57,13 @@ tags: -### 方法一 +### 方法一:递归 + +我们判断当前节点是否为空或者当前节点的值是否等于目标值,如果是则返回当前节点。 + +否则,如果当前节点的值大于目标值,则递归搜索左子树,否则递归搜索右子树。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。 @@ -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) ) ``` @@ -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); } } ``` @@ -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); } }; ``` @@ -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); } ``` diff --git a/solution/0700-0799/0700.Search in a Binary Search Tree/README_EN.md b/solution/0700-0799/0700.Search in a Binary Search Tree/README_EN.md index fb6fa50215c75..049b717c531f7 100644 --- a/solution/0700-0799/0700.Search in a Binary Search Tree/README_EN.md +++ b/solution/0700-0799/0700.Search in a Binary Search Tree/README_EN.md @@ -53,7 +53,13 @@ tags: -### 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. @@ -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) ) ``` @@ -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); } } ``` @@ -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); } }; ``` @@ -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); } ``` diff --git a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.cpp b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.cpp index 2c3786830adb9..ec18024c4f57c 100644 --- a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.cpp +++ b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.cpp @@ -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); } }; \ No newline at end of file diff --git a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go index 4ec2b8988d964..34e342c08b15c 100644 --- a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go +++ b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go @@ -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) } \ No newline at end of file diff --git a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.java b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.java index 409bc8c052cff..dc4c04e43792d 100644 --- a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.java +++ b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.java @@ -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); } } \ No newline at end of file diff --git a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py index 9c20585abdc3f..065a0025c62ee 100644 --- a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py +++ b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py @@ -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) ) diff --git a/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.ts b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.ts new file mode 100644 index 0000000000000..3be1e9eda77c6 --- /dev/null +++ b/solution/0700-0799/0700.Search in a Binary Search Tree/Solution.ts @@ -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); +} diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/README.md b/solution/0700-0799/0701.Insert into a Binary Search Tree/README.md index 0a970d21826eb..2d395f9605ec0 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/README.md +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/README.md @@ -65,7 +65,15 @@ tags: -### 方法一 +### 方法一:递归 + +如果根节点为空,我们直接创建一个新节点,值为 $\textit{val}$,并返回。 + +如果根节点的值大于 $\textit{val}$,我们递归地将 $\textit{val}$ 插入到左子树中,并将左子树的根节点更新为返回后的根节点。 + +如果根节点的值小于 $\textit{val}$,我们递归地将 $\textit{val}$ 插入到右子树中,并将右子树的根节点更新为返回后的根节点。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。 @@ -79,17 +87,14 @@ tags: # self.left = left # self.right = right class Solution: - def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: - def dfs(root): - if root is None: - return TreeNode(val) - if root.val < val: - root.right = dfs(root.right) - else: - root.left = dfs(root.left) - return root - - return dfs(root) + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + if root is None: + return TreeNode(val) + if root.val > val: + root.left = self.insertIntoBST(root.left, val) + else: + root.right = self.insertIntoBST(root.right, val) + return root ``` #### Java @@ -111,15 +116,14 @@ class Solution: * } */ class Solution { - public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null) { return new TreeNode(val); } - if (root.val < val) { - root.right = insertIntoBST(root.right, val); - } else { + if (root.val > val) { root.left = insertIntoBST(root.left, val); + } else { + root.right = insertIntoBST(root.right, val); } return root; } @@ -143,11 +147,14 @@ class Solution { class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { - if (!root) return new TreeNode(val); - if (root->val < val) - root->right = insertIntoBST(root->right, val); - else + if (!root) { + return new TreeNode(val); + } + if (root->val > val) { root->left = insertIntoBST(root->left, val); + } else { + root->right = insertIntoBST(root->right, val); + } return root; } }; @@ -168,10 +175,10 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { if root == nil { return &TreeNode{Val: val} } - if root.Val < val { - root.Right = insertIntoBST(root.Right, val) - } else { + if root.Val > val { root.Left = insertIntoBST(root.Left, val) + } else { + root.Right = insertIntoBST(root.Right, val) } return root } @@ -195,11 +202,14 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { */ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { - if (!root) return new TreeNode(val); - - if (val < root.val) root.left = insertIntoBST(root.left, val); - else root.right = insertIntoBST(root.right, val); - + if (!root) { + return new TreeNode(val); + } + if (root.val > val) { + root.left = insertIntoBST(root.left, val); + } else { + root.right = insertIntoBST(root.right, val); + } return root; } ``` diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/README_EN.md b/solution/0700-0799/0701.Insert into a Binary Search Tree/README_EN.md index 107ad4ecc1af6..79547b0563dda 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/README_EN.md +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/README_EN.md @@ -63,7 +63,15 @@ tags: -### Solution 1 +### Solution 1: Recursion + +If the root node is null, we directly create a new node with the value $\textit{val}$ and return it. + +If the root node's value is greater than $\textit{val}$, we recursively insert $\textit{val}$ into the left subtree and update the root of the left subtree with the returned root node. + +If the root node's value is less than $\textit{val}$, we recursively insert $\textit{val}$ into the right subtree and update the root of the right subtree with the returned root node. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. @@ -77,17 +85,14 @@ tags: # self.left = left # self.right = right class Solution: - def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: - def dfs(root): - if root is None: - return TreeNode(val) - if root.val < val: - root.right = dfs(root.right) - else: - root.left = dfs(root.left) - return root - - return dfs(root) + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + if root is None: + return TreeNode(val) + if root.val > val: + root.left = self.insertIntoBST(root.left, val) + else: + root.right = self.insertIntoBST(root.right, val) + return root ``` #### Java @@ -109,15 +114,14 @@ class Solution: * } */ class Solution { - public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null) { return new TreeNode(val); } - if (root.val < val) { - root.right = insertIntoBST(root.right, val); - } else { + if (root.val > val) { root.left = insertIntoBST(root.left, val); + } else { + root.right = insertIntoBST(root.right, val); } return root; } @@ -141,11 +145,14 @@ class Solution { class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { - if (!root) return new TreeNode(val); - if (root->val < val) - root->right = insertIntoBST(root->right, val); - else + if (!root) { + return new TreeNode(val); + } + if (root->val > val) { root->left = insertIntoBST(root->left, val); + } else { + root->right = insertIntoBST(root->right, val); + } return root; } }; @@ -166,10 +173,10 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { if root == nil { return &TreeNode{Val: val} } - if root.Val < val { - root.Right = insertIntoBST(root.Right, val) - } else { + if root.Val > val { root.Left = insertIntoBST(root.Left, val) + } else { + root.Right = insertIntoBST(root.Right, val) } return root } @@ -193,11 +200,14 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { */ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { - if (!root) return new TreeNode(val); - - if (val < root.val) root.left = insertIntoBST(root.left, val); - else root.right = insertIntoBST(root.right, val); - + if (!root) { + return new TreeNode(val); + } + if (root.val > val) { + root.left = insertIntoBST(root.left, val); + } else { + root.right = insertIntoBST(root.right, val); + } return root; } ``` diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp index ea17aff065736..0cc792fd39718 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp @@ -12,11 +12,14 @@ class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { - if (!root) return new TreeNode(val); - if (root->val < val) - root->right = insertIntoBST(root->right, val); - else + if (!root) { + return new TreeNode(val); + } + if (root->val > val) { root->left = insertIntoBST(root->left, val); + } else { + root->right = insertIntoBST(root->right, val); + } return root; } }; \ No newline at end of file diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.go b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.go index 8091ce51921a6..8ca9a7e14923d 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.go +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.go @@ -10,10 +10,10 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { if root == nil { return &TreeNode{Val: val} } - if root.Val < val { - root.Right = insertIntoBST(root.Right, val) - } else { + if root.Val > val { root.Left = insertIntoBST(root.Left, val) + } else { + root.Right = insertIntoBST(root.Right, val) } return root } \ No newline at end of file diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.java b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.java index ce930076c394a..233875d33bb81 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.java +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.java @@ -14,15 +14,14 @@ * } */ class Solution { - public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null) { return new TreeNode(val); } - if (root.val < val) { - root.right = insertIntoBST(root.right, val); - } else { + if (root.val > val) { root.left = insertIntoBST(root.left, val); + } else { + root.right = insertIntoBST(root.right, val); } return root; } diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.py b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.py index 2198fa541c579..7a9a5e9c2545c 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.py +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.py @@ -5,14 +5,11 @@ # self.left = left # self.right = right class Solution: - def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: - def dfs(root): - if root is None: - return TreeNode(val) - if root.val < val: - root.right = dfs(root.right) - else: - root.left = dfs(root.left) - return root - - return dfs(root) + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + if root is None: + return TreeNode(val) + if root.val > val: + root.left = self.insertIntoBST(root.left, val) + else: + root.right = self.insertIntoBST(root.right, val) + return root diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.ts b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.ts index 531f58a99bc77..e32722ba2d955 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.ts +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.ts @@ -13,10 +13,13 @@ */ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { - if (!root) return new TreeNode(val); - - if (val < root.val) root.left = insertIntoBST(root.left, val); - else root.right = insertIntoBST(root.right, val); - + if (!root) { + return new TreeNode(val); + } + if (root.val > val) { + root.left = insertIntoBST(root.left, val); + } else { + root.right = insertIntoBST(root.right, val); + } return root; }