problem 700

This commit is contained in:
Buduf 2022-04-14 13:42:54 +02:00
parent 92b6e2833e
commit b4fb50aeff

View 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;
}
};