Skip to content

Commit 5f4d9e5

Browse files
authored
Create README.md
1 parent 8d201d0 commit 5f4d9e5

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<h1 align='center'>Search - In a - Binary - Search - Tree</h1>
2+
3+
## Problem Statement
4+
5+
**Problem URL :** [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/description/)
6+
7+
![image](https://github.com/user-attachments/assets/7f1e0ee8-09f6-4ce1-80b8-96022eb73107)
8+
![image](https://github.com/user-attachments/assets/9e940976-e086-488c-be85-fbb099a09637)
9+
10+
## Problem Explanation
11+
Given a Binary Search Tree (BST) and a target value, the task is to locate the node in the BST that contains the target value. If the node exists, return the subtree rooted at that node. If the node does not exist in the tree, return `null`.
12+
13+
A **Binary Search Tree** has specific properties:
14+
- Each node’s left child contains values **less than** the node’s value.
15+
- Each node’s right child contains values **greater than** the node’s value.
16+
17+
Using these properties, we can efficiently search for the target value by comparing it with each node's value as we traverse the tree.
18+
19+
### Example 1
20+
21+
**Input:**
22+
- A BST with the structure:
23+
```
24+
4
25+
/ \
26+
2 7
27+
/ \
28+
1 3
29+
```
30+
- Target Value: `2`
31+
32+
**Output:**
33+
- A subtree rooted at the node with value `2`:
34+
```
35+
2
36+
/ \
37+
1 3
38+
```
39+
40+
**Explanation:**
41+
- The function starts at the root (node with value `4`).
42+
- Since `2` is less than `4`, we move to the left child (node with value `2`).
43+
- The current node’s value is `2`, which matches the target value.
44+
- So, the function returns the subtree rooted at this node, which includes `2` and its children `1` and `3`.
45+
46+
### Example 2
47+
48+
**Input:**
49+
- A BST with the structure:
50+
```
51+
4
52+
/ \
53+
2 7
54+
/ \
55+
1 3
56+
```
57+
- Target Value: `5`
58+
59+
**Output:**
60+
- `null`
61+
62+
**Explanation:**
63+
- The function starts at the root (node with value `4`).
64+
- Since `5` is greater than `4`, we move to the right child (node with value `7`).
65+
- The current node’s value is `7`, but `5` is less than `7`.
66+
- We move to the left child of `7`, but there is no left child, meaning we’ve reached a `null` node.
67+
- Since we couldn’t find the node with value `5`, the function returns `null`.
68+
69+
### Example 3
70+
71+
**Input:**
72+
- A BST with only the root node:
73+
```
74+
10
75+
```
76+
- Target Value: `10`
77+
78+
**Output:**
79+
- The subtree rooted at the node with value `10`:
80+
```
81+
10
82+
```
83+
84+
**Explanation:**
85+
- The target value matches the root node’s value, so the function returns the entire tree, which in this case is just the single node with value `10`.
86+
87+
### Key Points
88+
1. The function returns the subtree rooted at the target node if the target exists.
89+
2. If the target node is not found, the function returns `null`.
90+
3. By leveraging the properties of a BST, we can skip unnecessary comparisons, making this a more efficient search.
91+
92+
## Problem Solution
93+
```cpp
94+
class Solution {
95+
public:
96+
TreeNode* searchBST(TreeNode* root, int value) {
97+
if(root == NULL) return NULL;
98+
99+
if(root -> val == value) return root;
100+
101+
if(root -> val < value) return searchBST(root -> right, value);
102+
else return searchBST(root -> left, value);
103+
104+
return root;
105+
}
106+
};
107+
```
108+
109+
## Problem Solution Explanation
110+
1. **Class Declaration: `Solution`**
111+
```cpp
112+
class Solution {
113+
```
114+
- This line declares a class named `Solution`.
115+
- The purpose of this class is to hold the `searchBST` method, which performs a search in a Binary Search Tree (BST).
116+
- In LeetCode-style problems, functions are often encapsulated within a class for organizational purposes.
117+
118+
2. **Public Access Modifier**
119+
```cpp
120+
public:
121+
```
122+
- This is an access modifier. It allows the methods and members declared under it to be accessible from outside the class.
123+
- The `searchBST` method is declared under `public` to make it accessible to any code that creates an instance of `Solution`.
124+
125+
3. **Function Declaration: `searchBST`**
126+
```cpp
127+
TreeNode* searchBST(TreeNode* root, int value) {
128+
```
129+
- This line declares the `searchBST` function, which will search for a node containing the specified `value` in the BST.
130+
- It takes two arguments:
131+
- `TreeNode* root`: A pointer to the root node of the BST (or a subtree).
132+
- `int value`: The integer value that we want to locate in the BST.
133+
- The function returns a pointer to the `TreeNode` where the value is found. If the value is not found, it returns `NULL`.
134+
135+
4. **Base Case: Check if `root` is `NULL`**
136+
```cpp
137+
if(root == NULL) return NULL;
138+
```
139+
- This line checks if the `root` node is `NULL`.
140+
- If `root` is `NULL`, it means either the tree is empty or we've reached a leaf node without finding the target value.
141+
- In this case, the function returns `NULL` to indicate that the value does not exist in the tree.
142+
143+
5. **Check if the Current Node Contains the Target Value**
144+
```cpp
145+
if(root->val == value) return root;
146+
```
147+
- This line checks if the current `root` node's value is equal to the target `value`.
148+
- If they are equal, it means we have found the target node.
149+
- The function returns the pointer to the current node (`root`), which contains the target `value`.
150+
151+
6. **Decision to Search in the Right or Left Subtree**
152+
```cpp
153+
if(root->val < value) return searchBST(root->right, value);
154+
else return searchBST(root->left, value);
155+
```
156+
- **Condition `root->val < value`**:
157+
- This checks if the current node’s value is less than the target `value`.
158+
- Since this is a BST, if the current data is less than the target, the target must be in the **right subtree** (if it exists).
159+
- The function calls itself recursively on the right subtree (`root->right`) with the same target `value`.
160+
- This recursive call continues searching down the right branch until it either finds the node with the target value or reaches a `NULL` node.
161+
162+
- **`else` Condition**:
163+
- If the current node’s value is **greater than** the target `value`, the target must be in the **left subtree** (if it exists).
164+
- The function calls itself recursively on the left subtree (`root->left`) with the target `value`.
165+
- This recursive call continues the search down the left branch of the tree.
166+
167+
7. **Redundant `return root;` (Safeguard)**
168+
```cpp
169+
return root;
170+
```
171+
- This line is technically redundant because all possible cases (matching, left, and right subtree) have already been handled.
172+
- It will never actually be reached due to the previous `return` statements.
173+
- However, it serves as a safeguard to ensure that the function has a return statement in all code paths, even though it won’t be executed.
174+
175+
---
176+
177+
### Summary of Code Execution
178+
179+
- The `searchBST` function is designed to locate a specific node containing `value` in a BST.
180+
- The function uses the properties of a BST to reduce the search space by half at each step, making it efficient.
181+
- If the node with the target `value` is found, the function returns a pointer to that node.
182+
- If the function reaches a `NULL` pointer without finding the target value, it returns `NULL`, indicating that the value does not exist in the BST.
183+
184+
### Example Outputs
185+
186+
Let's see some example outputs for this function in action:
187+
188+
1. **Example 1**:
189+
- **Input**: `searchBST(root, 5)`
190+
- **Output**: Pointer to the node containing `5`
191+
- **Explanation**: The node with the value `5` exists in the BST, so the function returns a pointer to that node.
192+
193+
2. **Example 2**:
194+
- **Input**: `searchBST(root, 12)`
195+
- **Output**: `NULL`
196+
- **Explanation**: The value `12` is not present in the BST, so the function returns `NULL`.
197+
198+
3. **Example 3**:
199+
- **Input**: `searchBST(root, 10)`
200+
- **Output**: Pointer to the root node (assuming root has value `10`)
201+
- **Explanation**: The root node itself contains the value `10`, so the function returns the root node.
202+
203+
---
204+
205+
### Time and Space Complexity
206+
207+
- **Time Complexity**: \(O(h)\), where \(h\) is the height of the BST.
208+
- In the best case (balanced BST), the height \(h\) is \(O(\log n)\), where \(n\) is the number of nodes.
209+
- In the worst case (unbalanced BST, resembling a linked list), the height \(h\) is \(O(n)\).
210+
211+
- **Space Complexity**: \(O(h)\) for the recursive call stack.
212+
- In a balanced tree, this would be \(O(\log n)\), while in an unbalanced tree, it could be \(O(n)\).
213+
214+
---
215+
216+
This breakdown should provide a clear understanding of the code and its functionality.

0 commit comments

Comments
 (0)