mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
24
Easy/search-in-a-binary-search-tree/solution.cpp
Normal file
24
Easy/search-in-a-binary-search-tree/solution.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
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) {
|
||||
if (root == nullptr) {
|
||||
return nullptr;
|
||||
} else if (root->val > val) {
|
||||
return searchBST(root->left, val);
|
||||
} else if (root->val < val) {
|
||||
return searchBST(root->right, val);
|
||||
} else {
|
||||
return root;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user