[leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Example 1:
Input:
2
/ \
1 3
Output: true
Example 2:
5
/ \
1 4
/ \
3 6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
is 5 but its right child's value is 4.
Solution1: DFS
根据BST的性质: 左子树<根<右子树
要特别留心,对于input的root,没有办法给定一个确定的范围。所以初始化为null
5 (min, max) 5 :in(min,max)?
/ \ / / return true \ \ return false
1 4 1 update(min, 5), in (min, 5)? 4 update(5, 4), in (5,4)?
code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
/*why using null, null? 因为对于root,没有办法确定范围*/
return helper(root, null, null);
}
/* 因为上面定义的是null,这里initialize就应该是Integer而不是int*/
private boolean helper(TreeNode root, Integer min, Integer max) {
// base case
if (root == null)return true; // based on BST attribute
if ((min != null && root.val <= min) || (max != null && root.val >= max))
return false; // dfs
Boolean left = helper(root.left, min, root.val);
Boolean right = helper(root.right, root.val, max);
return left && right;
}
}
复杂度
Time: O(n) : visit each node
Space: O(h): h is the deepest height of such tree
[leetcode]98. Validate Binary Search Tree验证二叉搜索树的更多相关文章
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [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 ...
- 098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义: 左子树只包含小于当前节点的数. 右子树只包含大于当前节点的数. 所有子树自身必须也是二叉搜索树.示例 1 ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- PythonStudy——高级语言 High-level programming language
高级语言 高级语言(High-level programming language)相对于机器语言(machine language,是一种指令集的体系.这种指令集,称机器码(machine code ...
- easyUI默认图标的使用
使用格式如下: <table id="table" class="easyui-datagrid" style="width:600px;hei ...
- Day 11 函数对象,函数嵌套,作用域,闭包
函数总结 # 函数的定义:def func(a, b): print(a, b) return a + b# 函数四个组成部分# 函数名:调用函数的依据,必须的# 函数体:执行函数逻辑 ...
- Maven+Spirng+Mybatis+CXF搭建WebService服务
https://jingyan.baidu.com/article/39810a23b1de2fb637fda66c.html
- MySQL Point in Time Recovery the Right Way
In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly. Sometimes we nee ...
- bananapi+OLED做的一个打地鼠游戏,c语言编程
说明一下:BPI是对拍死的BPI的计数,对应最终的成绩RANK是难度 数值越低难度越高 每当打死10个BPI以后就会减一即难度高一级 默认初始化RANK等于15 DIE是存在的BPI数量,一旦数量大于 ...
- vue 项目全局修改element-ui的样式
引入了element-ui,但是和我们自己的样式颜色有很大的不同, 修改例子:在src文件下创建 element-var.scss,代码如下 $--color-primary: yellow; /* ...
- [二维码开发]二维码开发入门级demo
最近开发一个项目,涉及到二维码开发,于是乎就到网上找下直接可用的资源,遇到两个问题: 1.网上资源不够完整,找到完整的资源,需要下载分,这个你知道的 2.ThoughtWorks.QRCode版本不对 ...
- psi
purchase 采购sales 销售inventory 库存 outstock/instock/inventory taking outstock/instock/inventory 出库 入库 盘 ...
- jsonp原理及同源策略
[个人学习笔记,如有问题还请前辈纠正] jsonp 是用来跨域读取数据的,为什么从不同的域访问数据要用jsop呢?这源于一个著名的安全策略--同源策略,即: 协议.端口号.域名相同 举例说明:http ...