700. Search in a Binary Search Tree
# 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的更多相关文章
- 【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; ...
- 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 作者 ...
- [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. ...
- 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 ...
- [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 ...
- #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 ...
- 【LeetCode】700. Search in a Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 ...
随机推荐
- IIS 部署 Python Django网站流程(受够了野路子)
知道的,百度上搜出来的东西质量令人唏嘘.当你求助的时候多半还得靠自己,或者靠Google 介入正题,详细来一遍流程吧 当然,我是用Visual Studio 2019 来编辑开发Django项目的,如 ...
- JS(微信小程序)处理银行卡号
其实这是一个小程序的项目,但是JS还是那个JS 在本项目中要实现两种效果: 每隔四位插入空格: <view class='item_list'> <label>银行卡号:< ...
- 热血沙城-3.2移植-古月-cocos2dx源码
最近发现我去年学习2dx的时候移植过的一个游戏现在被放在网上出售 真是有点想笑 本人比较喜欢武侠风格的游戏,当时9秒开源了热血沙城 本着学习的态度 从2.1.2移植到3.2 用了一周的时间 中间各种 ...
- 润乾V4的最小化部署方式
在接触到的很多项目实际应用中,部署润乾V4都是使用润乾V4设计器自带的WEB发布向导,直接生成webRoot目录,然后将该目录下的所有文件COPY到项目目录下,然后修改web.xml文件和rep ...
- Pig类型转换
users.data的内容如下: lisg 28 75 dengsl 24 88 强制类型转换 users = load '/users.data' fehed = foreach users gen ...
- 初识oracle重做日志文件
转自 http://blog.csdn.net/indexman/article/details/7746948 以下易容翻译自oracle dba官方文档,不足之处还望指出. 管理重做日志文件 学习 ...
- 使用 sar 和 kSar 来发现 Linux 性能瓶颈
作者: Vivek Gite 译者: LCTT qhwdw | sar 命令用用收集.报告.或者保存 UNIX / Linux 系统的活动信息.它保存选择的计数器到操作系统的 /var/log/sa/ ...
- uml各类图
原文:http://www.cnblogs.com/way-peng/archive/2012/06/11/2544932.html 一.UML是什么?UML有什么用? 二.UML的历史 三.UML的 ...
- [C++] 用Xcode来写C++程序[1] 新建C++项目工程
用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...
- [转]CentOS7增加或修改SSH端口号
前言:开启某服务或软件的端口,要从该服务或软件监听的端口(多以修改配置文件为主),SeLinux和防火墙(FireWall)的安全策略下手.如果使用阿里云,腾讯等第三方服务器还需要对管理控制台的安全组 ...