**[Lintcode]Validate Binary Search Tree 验证二叉查找树
来源:互联网 发布:windows截屏怎么截图 编辑:程序博客网 时间:2024/05/18 05:53
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.
- A single node tree is a BST
An example:
2 / \1 4 / \ 3 5
The above binary tree is serialized as {2,1,4,#,#,3,5}
(in level order).
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** * @param root: The root of binary tree. * @return: True if the binary tree is BST, or false */ public boolean isValidBST(TreeNode root) { return helper(root, Integer.MIN_VALUE, Integer.MAX_VALUE); } boolean helper(TreeNode root, int minLimit, int maxLimit) { if(root == null) return true; if((minLimit != Integer.MIN_VALUE ? root.val > minLimit : true) && (maxLimit != Integer.MAX_VALUE ? root.val < maxLimit : true) && helper(root.left, minLimit, root.val) && helper(root.right, root.val, maxLimit)) return true; else return false; } }
0 0
- lintcode validate-binary-search-tree 验证二叉查找树
- **[Lintcode]Validate Binary Search Tree 验证二叉查找树
- Leetcode 98 Validate Binary Search Tree 验证二叉查找树
- 【LeetCode】Validate Binary Search Tree 验证二叉查找树
- 95.Validate Binary Search Tree-验证二叉查找树(中等题)
- 判断二叉查找树的合法性 Validate Binary Search Tree
- Validate Binary Search Tree 有效的二叉查找树
- 二叉查找树——Validate Binary Search Tree
- 【LeetCode】Validate Binary Search Tree 二叉查找树的判断
- lintcode:Validate Binary Search Tree
- 20150624 lintcode 总结 Validate Binary Search Tree
- Validate Binary Search Tree--判断一个树是不是二叉查找树(重重重)
- LeetCode OJ 之 Validate Binary Search Tree (验证二叉搜索树)
- 【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】
- Leetcode 98. Validate Binary Search Tree 验证二叉搜索树 解题报告
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- 二叉查找树(Binary Search Tree)
- 浅谈Jquery中的bind(),live(),delegate(),on()绑定事件方式
- 【UNITY资料】UNITY多语言化Localtion的工具 I2 Localization2.6.5 资料教程(二)
- Android 从Camera获取视频数据用SurfaceView显示
- RobotFrameWork(四)变量运算与Evaluate
- java 7 fork/join模糊图像示例, java 8 lambda 并行聚合函数 parallelSort()
- **[Lintcode]Validate Binary Search Tree 验证二叉查找树
- linux 常用命令
- java NIO Server 和 Client简单样例
- SecureCRT下载地址(包含破解程序)
- 实现C语言strcpy()函数
- Mysql性能调优策略
- windows 64 无法安装mysql-python
- 机器学习 - K近临法(KNN, k-Nearest Neighbor)
- 接口测试思路,进阶,方法,性能,安全,工具