Posted on

Description

Submission

/**
 * 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 == nullptr) return nullptr;
        if(root->val == val) return root;
        TreeNode* left = searchBST(root->left, val);
        if(left && left->val == val) return left;
        TreeNode* right = searchBST(root->right, val);
        if(right && right->val == val) return right;
        return nullptr;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *