problem 700
This commit is contained in:
parent
92b6e2833e
commit
b4fb50aeff
23
SearchInABinarySearchTree.cpp
Normal file
23
SearchInABinarySearchTree.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
struct TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode* left;
|
||||||
|
TreeNode* right;
|
||||||
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
TreeNode* searchBST(TreeNode* root, int val) {
|
||||||
|
while (root != nullptr && root->val != val) {
|
||||||
|
if (val < root->val) {
|
||||||
|
root = root->left;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
root = root->right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user