本文最后更新于619 天前,其中的信息可能已经过时,如有错误请发送邮件到tomding1065@gmail.com
视频讲解:https://www.bilibili.com/video/BV1wG411g7sF
CPP
/**
* 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) {
if(root == NULL || root->val == val)return root;
TreeNode* node = NULL;
if(val < root->val)node = searchBST(root->left , val);
if(val > root->val)node = searchBST(root->right , val);
return node;
}
};