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.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
""" if root:
def findsubtree(r,value):
if not r:
return None
elif r.val==value:
return r
else:
a=findsubtree(r.left,value)
if not a:
b=findsubtree(r.right,value)
return b
return a return findsubtree(root,val) return []

  

[LeetCode&Python] Problem 700. Search in a Binary Search Tree的更多相关文章

  1. [LeetCode&Python] Problem 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  2. [LeetCode&Python] Problem 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

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

  5. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

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

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

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

随机推荐

  1. Cglib方法实现动态代理

    除了使用JDK方式产生动态代理外,Java还给我们提供了另外一种产生动态代理的方法,那就是使用cglib. cglib是这样实现动态代理的: · ①.针对类来实现代理 · ②对指定目标类产生一个子类 ...

  2. Codeforces 912E - Prime Gift

    912E - Prime Gift 思路: 折半枚举+二分check 将素数分成两个集合(最好按奇偶位置来,保证两集合个数相近),这样每个集合枚举出来的小于1e18的积个数小于1e6. 然后二分答案, ...

  3. 构造函数用return 会出显什么情况

    首先我们都知道js中构造函数一般应该是这样的 function Super (a) { this.a = a; } Super.prototype.sayHello = function() { al ...

  4. 新概念 Lesson 5 How are you today

    How is Emma? 艾玛身体还好吗? 短语:very well How's Emma? She's very well, too. Emma is very well today   adv. ...

  5. Android之封装好的异步网络请求框架

    1.简介  Android中网络请求一般使用Apache HTTP Client或者采用HttpURLConnection,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使 ...

  6. android--------Popupwindow的使用

    PopupWindow在Android.widget包下,项目中经常会使用到PopupWindow做菜单选项,  PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容, ...

  7. Java 历史

    James Gosling 最初开始 Java 语言项目是在 1991 年的 7 月.Java 被用在他的许多 set-top box 工程中.这个语言最开始的时候被称为 Oka,这个是因为 Jame ...

  8. 用Javascript 实现倒计时

    用Javascript 实现倒计时<!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  9. TCP文件发送

    发送端(客户端) #include <iostream> #include <winsock2.h> #include <Ws2tcpip.h> #include ...

  10. Java容器涉及的类(代码)

    Customer: public class Customer implements Comparable{ private Integer customerId; private String cu ...