[leetcode]Validate Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/
题意:检测一颗二叉树是否是二叉查找树。
解题思路:看到二叉树我们首先想到需要进行递归来解决问题。这道题递归的比较巧妙。让我们来看下面一棵树:
4
/ \
2 6
/ \ / \
1 3 5 7
对于这棵树而言,怎样进行递归呢?root.left这棵树的所有节点值都小于root,root.right这棵树的所有节点值都大于root。然后依次递归下去就可以了。例如:如果这棵树是二叉查找树,那么左子树的节点值一定处于(负无穷,4)这个范围内,右子树的节点值一定处于(4,正无穷)这个范围内。思路到这一步,程序就不难写了。
代码:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def ValidBST(self, root, min, max):
if root == None:
return True
if root.val <= min or root.val >= max:
return False
return self.ValidBST(root.left, min, root.val) and self.ValidBST(root.right, root.val, max) def isValidBST(self, root):
return self.ValidBST(root, -2147483648, 2147483647)
[leetcode]Validate Binary Search Tree @ Python的更多相关文章
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- [LeetCode] Validate Binary Search Tree (两种解法)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Leetcode Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode: Validate Binary Search Tree [098]
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- LeetCode :: Validate Binary Search Tree[具体分析]
Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- Qt界面UI之QML初见(学习笔记四)
拖了大半年,今天终于有更新了...我自己都不好意思,最近太忙了! 今天讲一下:QML语法 一 概述 QML是一种专门用于构建用户界面的编程语言,它允许用户构建高性能,具有流畅特效的可视化应用程序,QM ...
- Bzoj5251 线段树+贪心
Bzoj5251 线段树+贪心 记录本蒟蒻省选后的第一篇题解!国际惯例的题面:首先这个东西显然是一棵树.如果我们把数值排序,并建立这棵树的dfs序,显然dfs序上的一个区间对应数值的一个区间,且根为数 ...
- java使用代理模拟http get请求
直接上代码: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.InetSocketAd ...
- tesseract-ocr识别中文扫描图片实例讲解
当我浏览http://code.google.com/p/tesseract-ocr并下载了几个文件下来之后顿时感到一头雾水,不知该如何下手.网上看到有人在linux操作系统下的实现, 如: 利用开源 ...
- 原生javascript封装的函数
1.javascript 加载的函数 window.onload = function(){} 2.封装的id函数 function $(id) { return document.getElemen ...
- FireDAC 下的 Sqlite [6] - 加密
主要就是设置 TFDConnection 的两个链接参数: Password, NewPassword, 非常简单. const dbPath = 'C:\Temp\SQLiteTest.sdb'; ...
- HDU 4790 Just Random (2013成都J题)
Just Random Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- How to create .gitignore file in Windows Explorer
How to create .gitignore file I need to add some rules to my .gitignore file, however, I can't find ...
- JavaScript学习总结(二十)——Javascript非构造函数的继承
一.什么是"非构造函数"的继承? 比如,现在有一个对象,叫做"中国人". var Chinese = { nation:'中国' }; 还有一个对象,叫做&qu ...
- 用Qemu搭建x86_64学习环境
作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 软件平台 主机: Ubuntu14.04 64位版本 模拟器:Qemu-2.8.0 Linux内核 ...