Skip to content

Commit 4692ee2

Browse files
authored
Create SearchInBST.cpp
1 parent 626a957 commit 4692ee2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

SearchInBST.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* searchBST(TreeNode* root, int val) {
15+
if(root==NULL){
16+
return root;
17+
}
18+
if(root->val==val){
19+
return root;
20+
}
21+
else if(root->val>val){
22+
return searchBST(root->left,val);
23+
}
24+
return searchBST(root->right,val);
25+
}
26+
};

0 commit comments

Comments
 (0)