# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if root==None : return NULL if root.val==val: return root elif root.val<val:
self.searchBST(root.right,val)
else:
self.searchBST(root.left,val)

问题:不明白树的数据结构在python中是如何实现的?

    def __init__(self, root_value):
self.root = root_value
self.leftchild = None
self.rightchild = None

终于看,明白了,如图:

上面的代码有问题:

class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if not root:
return None
if root.val == val:
return root
elif root.val < val:
return self.searchBST(root.right, val)
else:
return self.searchBST(root.left, val)
if x is None 只有None成立
if not x None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False
取上面的情况,就成立
if not x is None 相当于if not (x is None),推荐这种写法:if x is not None

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

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

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

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

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

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

  7. #Leetcode# 700. Search in a Binary Search Tree

    https://leetcode.com/problems/search-in-a-binary-search-tree/ Given the root node of a binary search ...

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

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

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

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

随机推荐

  1. 修改input被选中的默认样式

    input:focus{    outline: none;     border: 1px solid #fff; } 或者 input[type=text]:focus{   outline: n ...

  2. 关于vue2用vue-cli搭建环境后域名代理的http-proxy-middleware解决api接口跨域问题

    在vue中用http-proxy-middleware来进行接口代理,比如:本地运行环境为http://localhost:8080但真实访问的api为 http://www.baidu.com这时我 ...

  3. 【java错误】System.out.println()出错

    今天想测试java的System的类,没想到居然出错了.在同一个包下的java文件System全错,而其他包中的System没错.上网查了下资料,原来我是重定义了System类,覆盖了原来的Syste ...

  4. Android 对话框(Dialog)

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  5. 【Python】Java程序员学习Python(九)— 集合(list、tuple、range)和映射(dict)

    集合是一门语言最重要的一个特性,对集合支持的程度越好,使用起来越方便 一.表现形式 1.1 list list的基本用法: 使用中括号包裹,[1,2,3] 元素类型可以是任意类型,同样可以是list ...

  6. 第六章 函数、谓词、CASE表达式 6-2 谓词

    一.什么是谓词 需要满足返回值为真值的函数.谓词的返回值全都是真值(TRUE/ FALSE/UNKNOWN) 如:LIKE/BETWEEN /IS NULL/IS NOT NULL/IN/EXISTS ...

  7. 测试SDWebImage淡入淡出效果在UITableView中的重用显示问题

    测试SDWebImage淡入淡出效果在UITableView中的重用显示问题 这个是在上一篇教程的基础上所添加的测试环节! 效果图(从效果图中看是没有任何重用问题的): 源码: ImageCell.h ...

  8. Linux 环境部署记录(一) - 基础设定

    时间设置 查看系统当前日期/时间: date -R 查看系统硬件时钟: hwclock --show 设置硬件时间: hwclock --set --date="07/18/17 20:55 ...

  9. 铁乐学python_day21_面向对象编程3

    抽象类和接口类 以下内容大部分摘自博客http://www.cnblogs.com/Eva-J/ 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某 ...

  10. SQL Server 中的排名函数与使用场景

    1.RowNumber() Over (oder by.....)   在需要对某个不连续ID的表进行排序时使用 2.ROW_NUMBER() over(PARTITION by ...... ord ...