程式語言 - LeetCode - C - 700. Search in a Binary Search Tree



參考資訊:
https://www.cnblogs.com/Dylan-Java-NYC/p/16419214.html

題目:


解答:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* searchBST(struct TreeNode* root, int val)
{
    while (root) {
        if (root->val > val) {
            root = root->left;
        }
        else if (root->val < val) {
            root = root->right;
        }
        else {
            return root;
        }
    }

    return root;
}