llllmz

导航

700. 二叉搜索树中的搜索c

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

结果:

posted on 2024-03-06 13:33  神奇的萝卜丝  阅读(33)  评论(0)    收藏  举报