题目

给定二叉搜索树(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的更多相关文章

  1. 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 作者 ...

  2. 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 作者 ...

  3. 【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; ...

  4. [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. ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

  10. LintCode题解之Search Range in Binary Search Tree

    1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...

随机推荐

  1. vue脚手架 && 实例

    什么是vue脚手架? 即可以快速构建vue项目的工具,通过它,我们可以将vue项目所需要的文件等安装完成. 为什么要是用vue脚手架,优点何在? 因为创建一个vue项目需要很多的依赖,文件的设置等,而 ...

  2. Nginx主主负载均衡架构

    在和一些朋友交流Nginx+Keepalived技术时,我虽然已成功多次实Nginx+Keepaived项目方案,但这些都是用的单主Nginx在工作,从Nginx长期只是处于备份状态,所以我们想将二台 ...

  3. golang and mogodb

    1.golang的mogodb包下载:http://gopkg.in/mgo.v2    http://gopkg.in/mgo.v2/bson 2.golang的mongodb操作(mgo):htt ...

  4. 前台异步传过来的URL中获取token/获取string链接中的token

    1.链接例子: string url = "http://domainName:port/1/2/3/4.htm?sysCode=1001020&token=QXJzemR3YXlW ...

  5. Java中的阻塞队列-ArrayBlockingQueue(一)

    最近在看一些java基础的东西,看到了队列这章,打算对复习的一些知识点做一个笔记,也算是对自己思路的一个整理,本章先聊聊java中的阻塞队列 参考文章: http://ifeve.com/java-b ...

  6. ArrayList、Vector、HashMap、HashSet

    1. list  和 set 的有序无序是否可重复 List是有序可重复的: Set是无序不可重复的: 2.ArrayList.Vector.HashMap.HashSet的默认初始容量.加载因子.扩 ...

  7. AJAX跨域POST发送json时,会先发送一个OPTIONS预请求

    我们会发现,在很多post,put,delete等请求之前,会有一次options请求. 根本原因就是,W3C规范这样要求了!在跨域请求中,分为简单请求(get和部分post,post时content ...

  8. C++ Knowledge series 2

    Programming language evolves always along with Compiler's evolvement The semantics of constructors O ...

  9. 心得整理之一--RDLC多数据源多表

    我将项目中的一部分提炼出来,写了这个Demo. 先说一下需求, 从 API接口, 获取数据源, 调用RDLC 生成PDF文件. (后面还有涉及到使用福昕PDf阅读器进行设置文件自定义内容,以供外部程序 ...

  10. Java Map应用

    一.基本API使用方法 直接上代码,注释讲解 package com.map; import java.util.HashMap; import java.util.Iterator; import ...