LeetCode700. Search in a Binary Search Tree
题目
给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
例如,
给定二叉搜索树: 4
/ \
2 7
/ \
1 3 和值: 2你应该返回如下子树:
2
/ \
1 3在上述示例中,如果要找的值是
5,但因为没有节点值为5,我们应该返回NULL。
Tag
代码
1.递归 (一遍ac)
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(!root)
return nullptr;
return Helper(root,val);
}
TreeNode* Helper(TreeNode* root, int val)
{
if(root==nullptr) return root;
if(root->val==val) return root;
else if (root->val>val)
root=Helper(root->left,val);
else if (root->val <val)
root= Helper(root->right,val);
return root;
}
};
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(!root||val==root->val) return root;
if(root->val<val)
root=searchBST(root->right,val);
else if (root->val>val)
root =searchBST(root->left,val);
return root;
}
};
2.迭代
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while(root&&root->val!=val)
{
root= (root->val<val)? root->right: root->left;
}
return root;
}
};
问题
LeetCode700. Search in a Binary Search Tree的更多相关文章
- 04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- pat04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- 【Leetcode_easy】700. Search in a Binary Search Tree
problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...
- [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript
Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...
- [Swift]LeetCode700. 二叉搜索树中的搜索 | Search in a Binary Search Tree
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
- Search Range in Binary Search Tree
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...
- Lintcode: Search Range in Binary Search Tree
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...
- [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
- LeetCode 700 Search in a Binary Search Tree 解题报告
题目要求 Given the root node of a binary search tree (BST) and a value. You need to find the node in the ...
- LintCode题解之Search Range in Binary Search Tree
1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...
随机推荐
- gcc 4.9编译
参考 http://blog.csdn.net/hzhxxx/article/details/28634893
- 《Flink 源码解析》—— 源码编译运行
更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...
- CSS伪类:first-child与:first-of-type的异同
CSS里关于元素匹配里面有两个非常类似却又不尽相同的选择器,伪类 :first-child 和 :first-of-type 两者在匹配方式上有很大差异,其实在一开始自己也没去注意这个细节,直到上次一 ...
- SWIG 和 Python——c/c++与脚本交互
C 和 C++ 被公认为(理当如此)创建高性能代码的首选平台.对开发人员的一个常见要求是向脚本语言接口公开 C/C++ 代码,这正是 Simplified Wrapper and Interface ...
- Python 科学工具使用
Python 科学工具笔记 numpy a = numpy.array([1,2,3,4]);// 创建一个numpy的数组对象 此时a.shape显示的值为(4,); 由此得出结论在一维的数组中, ...
- input type="image" 提交表单
提到<input type="image" />,说起来有些惭愧.之前的工作基本每周都要制作两到三个注册用户的网页.其中就用它提交表单. 那个时候我想当然的以为这是用 ...
- ArcMap如何修改地图坐标系统
有时候,地图投影坐标需要作出修改,使得符合要求,不然空间参考不一样无法进行进一步的操作,分析等!下面介绍arcgis地图投影坐标的修改! 1.首先,将地图数据导入,这里我导入的是广西的边界图bound ...
- 【Android学习入门】Android中activity的启动模式
启动模式简单地说就是Activity启动时的策略,在Androidmanifest.xml文件中的标签android:launchMode属性设置,在Android中Activity共有四种启动模式分 ...
- 常见O/R框架介绍
1.hibernate(JPA的一个实现,同时也有自己的特色)2.toplink3.jdo4.ibatis 4.JPA a)意愿统一天下
- hibernate 模拟实现和What is and Why O/R Mapping
What is and Why O/R Mapping What is : 用面向对象的方式调用api,类库帮我们翻译成面向关系的方式. Why: 1.JDBC操作数据库很繁琐2.Sql 语句编写并不 ...