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 ...
随机推荐
- LeetCode SQL:Employees Earning More Than Their Managers
# Write your MySQL query statement below SELECT a.Name FROM Employee AS a INNER JOIN Employee AS b O ...
- K先生的博客
努力,不是为了要感动谁,也不是要做给哪个人看,而是要让自己随时有能力跳出自己厌恶的圈子,并拥有选择的权利. 自己既然选择了这条路,那就要不忘初心坚定的走下去!或许坚持到最后自己会伤痕累累,但,那又怎么 ...
- Aspose.cells常用用法1
代码: var execl_path = @"G:\zhyue\backup\项目修改-工作日常\2018-11-12 区域楼盘中心点和放大比例计算\a.xlsx"; Workbo ...
- elixir 模块
模块定义 defmodule 函数定义 def 私有函数 defp --相当于其他语言 private iex(29)> defmodule Math do...(29)> def ...
- 【vue】vue的路由权限管理
前言: 最近闲来无事浏览各种博客,看到了一个关于路由权限的管理,觉得很有用,针对那个博客,准备自己写一个demo. 实现: 路由大致分为用户路由<特定用户才能浏览>和基本路由<所有用 ...
- Difference between model.evaluate vs model.predict in Keras
The model.evaluate function predicts the output for the given input and then computes the metrics ...
- apache url rewrite及正则表达式笔记
什么是mod_rewrite? mod_rewrite是apache一个允许服务器端对请求url做修改的模块.入端url将和一系列的rule来比对.这些rule包含一个正则表达式以便检测每个特别的模式 ...
- Oracle EBS 应收API只创建收款没有核销行以及消息堆栈
只创建了收款但没有创建核销行 排除其他原因 有可能是缓存溢出导致的这个要改成true 且使用消息堆栈处理
- Struts-config.xml配置文件《action-mappings》元素的详解
原文地址:http://blog.163.com/sara1124@126/blog/static/11291097020105125537114/ action-mappings 该元素用于将Act ...
- Linux 安装 pycharm
1.Windows系统下载http://www.jetbrains.com/pycharm/download/#section=linux2.解压到挂载文件夹 mount -t cifs -o use ...