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 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.
题目分析及思路
题目给出一棵二叉树和一个数值,要求找到与该数值相等的结点并返回以该结点为根结点的子树。可以使用队列保存结点,再将结点循环弹出进行判断。
python代码
# 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
"""
q = collections.deque()
q.append(root)
while q:
node = q.popleft()
if not node:
continue
if node.val != val:
q.append(node.left)
q.append(node.right)
continue
else:
return node
return None
最新文章
- ubuntu 安装 vmware 12
- 单词words
- Makefile 一点一滴(三)—— 尝试简单的变量替换
- 刷了OpenWrt Attitude Adjustment 12.09,很满意
- 做一些Spring AOP做过的事,封装 jdk动态代理成为一个黑盒子
- div里嵌套了img底部会有白块问题和图片一像素问题解决
- 为网站添加网址图标favicon.ico
- 利用apktool反编译apk
- 李洪强iOS开发支付集成之微信支付
- python高级编程(第12章:优化学习)3
- 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
- ASP.NET MVC 使用MSBuild部署的几个注意事项
- hdu_5873_Football Games(xjb搞)
- docke 基本安装使用
- Java设计模式之《模板模式》及使用场景
- 爬虫--工具安装Jupyter anaconda
- ALSA声卡驱动的DAPM(二)-建立过程
- Vue.js项目脚手架构建
- css3 实现图片等比例放大与缩小
- PDB自动启动以及Oracle Pfile的参数修改示范
热门文章
- 《软件测试自动化之道》读书笔记 之 底层的Web UI 测试
- 推荐几个Windows工具软件: Stickies - 桌面贴
- 【iCore4 双核心板_ARM】例程一:ARM驱动三色LED
- 使用Fidder将生成环境代码映射到本地(文件夹)
- halcon模板匹配
- mycat 9066管理端口
- Oracle清理回收站的方法
- python 简单的server请求
- java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
- [Stats385] Lecture 03, Harmonic Analysis of Deep CNN