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;
}
结果:

浙公网安备 33010602011771号