Leetcode 783 Solution
This article provides solution to leetcode question 783 (search-in-a-binary-search-tree)
Access this page by simply typing in "lcs 783" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/search-in-a-binary-search-tree
Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == NULL)
return NULL;
if (root->val > val)
return searchBST(root->left, val);
else if (root->val < val)
return searchBST(root->right, val);
else
return root;
}
};