leetcode 700. Search in a Binary Search Tree

/**
 * 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->val==val)return root;
        TreeNode* p;
        p = root;
        while(p!=NULL&&p->val!=val)
        {
            if(p->val>val)p=p->left;
            else if(p->valright;
        }
        return p;
    }
};

 

你可能感兴趣的:(leetcode)