题目要求

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

最新文章

  1. ubuntu 安装 vmware 12
  2. 单词words
  3. Makefile 一点一滴(三)—— 尝试简单的变量替换
  4. 刷了OpenWrt Attitude Adjustment 12.09,很满意
  5. 做一些Spring AOP做过的事,封装 jdk动态代理成为一个黑盒子
  6. div里嵌套了img底部会有白块问题和图片一像素问题解决
  7. 为网站添加网址图标favicon.ico
  8. 利用apktool反编译apk
  9. 李洪强iOS开发支付集成之微信支付
  10. python高级编程(第12章:优化学习)3
  11. 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
  12. ASP.NET MVC 使用MSBuild部署的几个注意事项
  13. hdu_5873_Football Games(xjb搞)
  14. docke 基本安装使用
  15. Java设计模式之《模板模式》及使用场景
  16. 爬虫--工具安装Jupyter anaconda
  17. ALSA声卡驱动的DAPM(二)-建立过程
  18. Vue.js项目脚手架构建
  19. css3 实现图片等比例放大与缩小
  20. PDB自动启动以及Oracle Pfile的参数修改示范

热门文章

  1. 《软件测试自动化之道》读书笔记 之 底层的Web UI 测试
  2. 推荐几个Windows工具软件: Stickies - 桌面贴
  3. 【iCore4 双核心板_ARM】例程一:ARM驱动三色LED
  4. 使用Fidder将生成环境代码映射到本地(文件夹)
  5. halcon模板匹配
  6. mycat 9066管理端口
  7. Oracle清理回收站的方法
  8. python 简单的server请求
  9. java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
  10. [Stats385] Lecture 03, Harmonic Analysis of Deep CNN