LeetCode June Challenge - Day 15

Problem: LeetCode June Challenge - Day 15

Ideas:

  1. If the current node is greater than the value searched, then the value searched can’t be in the right subtree, where all elements are greater, and viceversa.

Solution:

class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        while(root != NULL && root->val != val)
            root = (root->val > val ? root->left : root->right);
        return root;
    }
};