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 ...
随机推荐
- POJ 3522 ——Slim Span——————【最小生成树、最大边与最小边最小】
Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7102 Accepted: 3761 Descrip ...
- 高精度运算——java
java大法 java的框架. import java.io.*; import java.util.*; import java.math.*; public class Main{ public ...
- CentOS搭建KMS服务器
安装 使用命令: #CentOS,Redhat,Fedora等请选择CentOS脚本 wget https://raw.githubusercontent.com/dakkidaze/one-key- ...
- Ajax提交表单数据(包含文件)
1. 表单数据->JSON->后台 2. 表单序列化[方式一] jquery.serializejson.js <script src="/js/jquery.serial ...
- Akka探索第二个例子by fsharp
本文重度借鉴了github上akkabootcamp教程. 先上代码 open Akka open Akka.Actor open System type Message = | ContinuePr ...
- 连接数据库报错:1130-Host 'xxx' is not allowed to connect to this MySQL server解决
出现这个问题的同学都很奇怪,为啥用localhost就可以连接上,但是使用本地ip就不行.出现这个问题的原因就是mysql未开启mysql远程访问权限导致. 这时候我们就用cmd去访问下你的mysql ...
- one + two = 3
读入两个小于100的正整数A和B,计算A+B.需要注意的是:A和B的每一位数字由对应的英文单词给出. 输入 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =", ...
- 微信小程序电商实战-首页(下)
好了,上一期我们把首页搜索.导航栏和广告轮播给做完了,那么接下来会继续完成我们首页的剩余部分,先看我们要实现的效果吧! 本期实现效果图.gif 本期我们要实现首页的实时热销榜.福利专场和左下方个人 ...
- Miner3D Basic基础版
——强大的数据可视化软件 数据分析并不很复杂,Miner3D Basic基础版首先使用简单的方法,创造了强劲的图形驱动的数据处理模型,然后通过一个完整的视图为基本的交互环境,对基本数据进行分析,并通过 ...
- 诸葛io | 精细化运营分析解决方案
类型: 定制服务 软件包: business intelligence internet media solution collateral 联系服务商 产品详情 解决方案 概要 数据监测 ? 异常发 ...