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 ...
随机推荐
- Transform控制的物体移动
Transform控制的物体移动:public class TKMove : MonoBehaviour { public float HSpeed; public float VSpeed; pri ...
- axios处理http请求
axios中文文档 https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format axios文档 ...
- 使用codedom自动生成代码
刚刚接触自动代码生成,便小试牛刀,解决了项目中的一些问题. 问题:我们的项目分成很多层次,当增加一个方法的时候,会显得异常繁琐,但每个层次之间的调用大同小异,所以尝试使用代码生成.现在假设有Engin ...
- Linux小知识(1): bash中执行数据库的相关操作
工作中经常会遇到命令行操作数据库或登陆至其他服务器等情况,往往需要通过命令行进入相关的数据库或登陆至相关的服务器,再执行指令操作,因此有没有方法,在命令行或bash脚本中模拟进入数据库或登陆服务器操作 ...
- HDU 4357——String change——————【规律题】
String change Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 从零开始的全栈工程师——js篇2.21(事件对象 arguments 阻止事件默认行为兼容 事件委托 事件源对象)
一.事件对象 1.常用的事件2.每个元素身上的事件都是天生存在的 不需要我们去定义 只需要我们给这个事件绑定一个方法 当事件触发的时候就会执行这个方法 3.事件绑定的写法 ①div.onclick=f ...
- 监控系统 - mk-livestatus
/usr/bin/unixcat /usr/lib64/check_mk/livestatus.o /etc/xinetd.d/livestatus service livestatus { type ...
- 在Sql Server中使用Guid类型的列及设置Guid类型的默认值
1.列的类型为uniqueidentifier 2.列的默认值可以设为newid()
- BIN文件对象数据库,直接存储对象做数据库,小型项目用它准没错
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- vos话务抽查小方法
问题: 假如现在公司落地前缀主要有三个分别如下: 9150:主要用来走正规量 880:用来走正规量 660:全业务(合法) 已知: 非法业务量主要有:博彩,发票,白银,黄金,期货 合法非正规量有:保健 ...