leetcode 700. Search in a Binary Search Tree
程序员文章站
2022-06-08 09:31:00
...
/**
* 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->val<val)p=p->right;
}
return p;
}
};
上一篇: php中3种方法删除字符串中间的空格
下一篇: php分页函数示例代码
推荐阅读
-
荐 Java刷题笔记15:不同的二叉搜索树( Unique Binary Search Trees)
-
leetcode笔记:Invert Binary Tree
-
Convert Sorted Array to Binary Search Tree
-
二分查找(binary search)java实现及时间复杂度
-
【leetcode】-700. Search in a Binary Search Tree 查找二叉搜索树
-
LeetCode 33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II
-
Leetcode 33 Search in Rotated Sorted Array
-
LeetCode·33. Search in Rotated Sorted Array
-
leetcode 33. Search in Rotated Sorted Array
-
LeetCode------Search in Rotated Sorted Array