✅ BEATS ALL C++ SOLUTION ☑️
PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT\n# CONNECT WITH ME\n### https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n#### https://www.instagram.com/pratay_nand
# **PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT**\n# **CONNECT WITH ME**\n### **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n#### **[https://www.instagram.com/pratay_nandy/]()**\n\n# Approach\nCertainly! Let\'s go through the code line by line:\n\n\n```cpp\n        if(root==NULL)return NULL;\n```\n\n- **Null Check:** The function starts with a check for a base case. If the current node (`root`) is `NULL`, it means that the tree (or subtree) does not contain the target value. In this case, the function immediately returns `NULL`.\n\n```cpp\n        if(root->val==val)return root;\n```\n\n- **Node Found:** If the value of the current node (`root->val`) is equal to the target value `val`, the function returns the current node (`root`) as it has found the node with the desired value.\n\n```cpp\n        if(root->val>val)return searchBST(root->left,val);\n```\n\n- **Recursive Search (Left):** If the target value is less than the current node\'s value, the function calls itself recursively with the left subtree (`root->left`). This is because in a BST, values less than the current node are present in the left subtree.\n\n```cpp\n        if(root->val<val)return searchBST(root->right,val);\n```\n\n- **Recursive Search (Right):** If the target value is greater than the current node\'s value, the function calls itself recursively with the right subtree (`root->right`). This is because in a BST, values greater than the current node are present in the right subtree.\n\n```cpp\n        return NULL;\n```\n\n- **Default Return:** If none of the conditions are met, the function returns `NULL`. This statement is reached when the entire subtree has been traversed, and the target value was not found.\n\n```cpp\n    }\n};\n```\n\nThis function uses a recursive approach to search in a binary search tree. It checks the base case (current node is `NULL`), handles the case where the current node\'s value matches the target value, and recursively explores either the left or right subtree based on the comparison of the target value with the current node\'s value.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:**0(N)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:**0(1)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![upvote2.png](https://assets.leetcode.com/users/images/62bafb97-0420-408f-802f-03e74f9120ea_1704896346.7604942.png)\n\n# Code\n```\nclass Solution {\npublic:\n    TreeNode* searchBST(TreeNode* root, int val) {\n        if(root==NULL)return NULL;\n        if(root->val==val)return root;\n        if(root->val>val)return searchBST(root->left,val);\n        if(root->val<val)return searchBST(root->right,val);\n        return NULL;\n    }\n};\n```
['Tree', 'Binary Search Tree', 'Binary Tree', 'C++']