|
| 1 | +Here's the full LeetCode-style `README.md` file for **700. Search in a Binary Search Tree**, matching the style and structure you've requested: |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +```md |
| 6 | +--- |
| 7 | +comments: true |
| 8 | +difficulty: Easy |
| 9 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0700.Search%20in%20a%20Binary%20Search%20Tree/README_EN.md |
| 10 | +tags: |
| 11 | + - Tree |
| 12 | + - Binary Search Tree |
| 13 | + - Binary Tree |
| 14 | +--- |
| 15 | + |
| 16 | +<!-- problem:start --> |
| 17 | + |
| 18 | +# [700. Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) |
| 19 | + |
| 20 | +[中文文档](/solution/0700-0799/0700.Search%20in%20a%20Binary%20Search%20Tree/README.md) |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +<!-- description:start --> |
| 25 | + |
| 26 | +You are given the `root` of a binary search tree (BST) and an integer `val`. |
| 27 | + |
| 28 | +Find the node in the BST whose value equals `val` and return the subtree rooted with that node. If such a node does not exist, return `null`. |
| 29 | + |
| 30 | +<!-- description:end --> |
| 31 | + |
| 32 | +## Examples |
| 33 | + |
| 34 | +<!-- examples:start --> |
| 35 | + |
| 36 | +### Example 1: |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +``` |
| 41 | +Input: root = [4,2,7,1,3], val = 2 |
| 42 | +Output: [2,1,3] |
| 43 | +``` |
| 44 | +
|
| 45 | +### Example 2: |
| 46 | +
|
| 47 | + |
| 48 | +
|
| 49 | +``` |
| 50 | +Input: root = [4,2,7,1,3], val = 5 |
| 51 | +Output: [] |
| 52 | +``` |
| 53 | +
|
| 54 | +<!-- examples:end --> |
| 55 | +
|
| 56 | +## Constraints |
| 57 | +
|
| 58 | +- The number of nodes in the tree is in the range `[1, 5000]`. |
| 59 | +- `1 <= Node.val <= 10⁷` |
| 60 | +- `root` is a binary search tree. |
| 61 | +- `1 <= val <= 10⁷` |
| 62 | +
|
| 63 | +## Solutions |
| 64 | +
|
| 65 | +<!-- solution:start --> |
| 66 | +
|
| 67 | +### Solution 1: Recursion |
| 68 | +
|
| 69 | +Since the tree is a binary search tree, we can utilize the BST property: |
| 70 | +
|
| 71 | +- If `val` < `root.val`, we search the left subtree. |
| 72 | +- If `val` > `root.val`, we search the right subtree. |
| 73 | +- If `val` == `root.val`, we return the node. |
| 74 | +
|
| 75 | +This approach has: |
| 76 | +
|
| 77 | +- Time Complexity: O(h), where `h` is the height of the tree. |
| 78 | +- Space Complexity: O(h), due to recursive call stack. |
| 79 | +
|
| 80 | +<!-- tabs:start --> |
| 81 | +
|
| 82 | +### Python3 |
| 83 | +
|
| 84 | +```python |
| 85 | +# Definition for a binary tree node. |
| 86 | +# class TreeNode: |
| 87 | +# def __init__(self, val=0, left=None, right=None): |
| 88 | +# self.val = val |
| 89 | +# self.left = left |
| 90 | +# self.right = right |
| 91 | +class Solution: |
| 92 | + def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: |
| 93 | + if root is None or root.val == val: |
| 94 | + return root |
| 95 | + return self.searchBST(root.left, val) if val < root.val else self.searchBST(root.right, val) |
| 96 | +``` |
| 97 | + |
| 98 | +### Java |
| 99 | + |
| 100 | +```java |
| 101 | +/** |
| 102 | + * Definition for a binary tree node. |
| 103 | + * public class TreeNode { |
| 104 | + * int val; |
| 105 | + * TreeNode left; |
| 106 | + * TreeNode right; |
| 107 | + * TreeNode() {} |
| 108 | + * TreeNode(int val) { this.val = val; } |
| 109 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 110 | + * this.val = val; |
| 111 | + * this.left = left; |
| 112 | + * this.right = right; |
| 113 | + * } |
| 114 | + * } |
| 115 | + */ |
| 116 | +class Solution { |
| 117 | + public TreeNode searchBST(TreeNode root, int val) { |
| 118 | + if (root == null || root.val == val) return root; |
| 119 | + return val < root.val ? searchBST(root.left, val) : searchBST(root.right, val); |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### C++ |
| 125 | + |
| 126 | +```cpp |
| 127 | +/** |
| 128 | + * Definition for a binary tree node. |
| 129 | + * struct TreeNode { |
| 130 | + * int val; |
| 131 | + * TreeNode *left; |
| 132 | + * TreeNode *right; |
| 133 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 134 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 135 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 136 | + * }; |
| 137 | + */ |
| 138 | +class Solution { |
| 139 | +public: |
| 140 | + TreeNode* searchBST(TreeNode* root, int val) { |
| 141 | + if (!root || root->val == val) return root; |
| 142 | + return val < root->val ? searchBST(root->left, val) : searchBST(root->right, val); |
| 143 | + } |
| 144 | +}; |
| 145 | +``` |
| 146 | +
|
| 147 | +### Go |
| 148 | +
|
| 149 | +```go |
| 150 | +/** |
| 151 | + * Definition for a binary tree node. |
| 152 | + * type TreeNode struct { |
| 153 | + * Val int |
| 154 | + * Left *TreeNode |
| 155 | + * Right *TreeNode |
| 156 | + * } |
| 157 | + */ |
| 158 | +func searchBST(root *TreeNode, val int) *TreeNode { |
| 159 | + if root == nil || root.Val == val { |
| 160 | + return root |
| 161 | + } |
| 162 | + if val < root.Val { |
| 163 | + return searchBST(root.Left, val) |
| 164 | + } |
| 165 | + return searchBST(root.Right, val) |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +### TypeScript |
| 170 | + |
| 171 | +```ts |
| 172 | +/** |
| 173 | + * Definition for a binary tree node. |
| 174 | + * class TreeNode { |
| 175 | + * val: number |
| 176 | + * left: TreeNode | null |
| 177 | + * right: TreeNode | null |
| 178 | + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { |
| 179 | + * this.val = (val===undefined ? 0 : val) |
| 180 | + * this.left = (left===undefined ? null : left) |
| 181 | + * this.right = (right===undefined ? null : right) |
| 182 | + * } |
| 183 | + * } |
| 184 | + */ |
| 185 | + |
| 186 | +function searchBST(root: TreeNode | null, val: number): TreeNode | null { |
| 187 | + if (root === null || root.val === val) { |
| 188 | + return root; |
| 189 | + } |
| 190 | + return val < root.val ? searchBST(root.left, val) : searchBST(root.right, val); |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +<!-- tabs:end --> |
| 195 | + |
| 196 | +<!-- solution:end --> |
| 197 | + |
| 198 | +<!-- problem:end --> |
| 199 | +``` |
| 200 | +
|
| 201 | +Let me know if you want the iterative version too or a DFS/BFS variant! |
0 commit comments