题目链接:https://leetcode-cn.com/problems/search-in-a-binary-search-tree/
题目如下:
/**
* Definition for a binary tree node.
* 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) {
TreeNode* result;
if(!root) return NULL;
if(root->val==val) return root;
else{
if((result=searchBST(root->left,val))==NULL)
result=searchBST(root->right,val);
}
return result;
}
};