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 ...
随机推荐
- 【学习笔记】--- 老男孩学Python,day10, 初识函数 形参、实参
函数:对功能的封装语法: def 函数名(形参): 函数体 函数名(实参) 函数名:命名规则和变量一样 函数的返回值: return, 函数执行完毕. 不会执行后面逻辑 1. 如果函数中不写retur ...
- Code Signal_练习题_adjacentElementsProduct
Given an array of integers, find the pair of adjacent elements that has the largest product and retu ...
- js-数组中查找特定元素并返回所有该元素的索引
//在数组中查找所有出现的x,并返回一个包含匹配索引的数组 function findall(a,x){ var results=[], len=a.length, pos=0; while(pos& ...
- Java反射机制动态代理
1.什么事反射机制动态代理 在一段代码的前后动态执行其他操作,比如有一个方法是往数据库添加一个记录,我们可以通过动态代理,在操作数据库方法的前和后添加代码执行打开数据库连接和关闭数据库连接. 2.演示 ...
- Pig UDF 用户自定义函数
注册UDF do.pig的内容如下: register /xx/yy.jar data = load 'data'; result = foreach data generate aa.bb.Uppe ...
- responsebody和requestbody的使用
Controller的方法上加了一个@ResponseBody,那么他的作用是什么呢?/** * 新增或修改一条对象. */ @RequestMapping("/save_field&quo ...
- 神奇的负Margin
在做slide时候一般都是采用父元素超宽+overflow的做法,今天发现了用margin-right:-100%;可以让子元素全部重叠起来.效果也是不错的
- 【Java】生成UUID
import java.util.UUID; public class MainProcess { public static void main(String[] args) { UUID uuid ...
- Leetcode 回文数字判断
一.问题描述 判断一个integer 型的数字是否是回文,空间复杂度应该是常数级别的 . 二.问题分析 首先,负数不是回文,10的整数倍不会是回文,个位数一定是回文. 三.代码实现 思路:将一个数字翻 ...
- Hive的介绍及安装
简介 Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件 映射为一张数据库表,并提供类 SQL 查询功能. 本质是将 SQL 转换为 MapReduce 程序. Hive组件 ...