https://leetcode.com/problems/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 that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.

For example,

Given the tree:
4
/ \
2 7
/ \
1 3 And the value to search: 2

You should return this subtree:

      2
/ \
1 3

In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.

Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.

代码:

/**
* 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) return NULL;
while(root) {
if(root -> val == val)
return root;
else if(val > root -> val)
root = root -> right;
else root = root -> left;
} return NULL;
}
};

  虽然是 Easy 但是自己 1A 就很酥胡

#Leetcode# 700. Search in a Binary Search Tree的更多相关文章

  1. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

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

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

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

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

  6. 【LeetCode】700. Search in a Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

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

  8. [LeetCode&Python] Problem 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 BST ...

  9. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

随机推荐

  1. PHP使用阿里大鱼发送短信验证

    目前,基本上所有的网站注册都要求手机绑定,并通过下发短信验证码方式验证手机的真实性,提高了用户的真实性.但是一般企业单独申请短信行业通道都比较困难,因此选择一家信誉好,稳定性.及时性强的第三方短信通道 ...

  2. Zookeeper -- 关于Zookeeper

    Zookeeper是什么? 分布式协调框架 Zookeeper中文件呈树形结构,树形结构下包含多个节点,称为Znode:zk中节点存储数据不超过1M,指得是Znode中存储数据不超过1M Zookee ...

  3. jz2440_lcd

    VDEN      使能信号 HSYNC    水平方向的同步信号 VSYNC    垂直方向的同步信号 LED-/LED+   背光信号 VCLK       时钟信号 VD0~VD23    数字 ...

  4. Python学习:7.文件操作

    文件操作 我们曾将听过一个问题,将大象放入冰箱分为三步:1.打开冰箱门,2.将大象放进去,3.关上冰箱门.今天我们要讲的Python文件操作的步骤就像将大象放入冰箱的步骤一样. 使用Python操作文 ...

  5. c指针学习

    #include <stdio.h> void main() { int a1=10; int a2=11; int * pa1, * pa2; pa1 = &a1; pa2 = ...

  6. 我和Python的Py交易》》》》》》 命名空间的小弟作用域

    命名空间的小弟作用域 在这要明确一个观点,在Python中万物皆对象,而变量指向的就是对象. 变量可以是 类名,函数名,储存数据的变量…… 对象可以是  类 ,被封装的一段代码(函数),数据…… 命名 ...

  7. CodeChef BIBOARD: Binary Board 命题报告

    这道题当时有了一点模糊的想法之后,构思了一整天-- 题意: 有一\(N \times M\)网格,每一格可以是白色或黑色.令\(B_i\)表示\(i \times i\)的纯黑子网格数量(子网格是指原 ...

  8. msys2-x86_64 Mingw64 编译openssl

    windows 刚开始编译时提示找不到gcc 添加环境变量export PATH=$PATH:/mingw64/bin$source /etc/profile 将openssl源码复制到C:\msys ...

  9. 【BZOJ5299】【CQOI2018】解锁屏幕(动态规划,状态压缩)

    [BZOJ5299][CQOI2018]解锁屏幕(动态规划,状态压缩) 题面 BZOJ 洛谷 Description 使用过Android手机的同学一定对手势解锁屏幕不陌生.Android的解锁屏幕由 ...

  10. 1030: [JSOI2007]文本生成器

    1030: [JSOI2007]文本生成器 https://www.lydsy.com/JudgeOnline/problem.php?id=1030 分析: AC自动机+dp. 正难则反,求满足的, ...