LeetCode - Validate Binary Search Tree
2013-09-25 14:01
579 查看
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.
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.
bool check(TreeNode* root, int minValue, int maxValue) { if (NULL == root) { return true; } return (root->val > minValue) && (root->val < maxValue) && check(root->left, minValue, root->val) && check(root->right, root->val, maxValue); } bool isValidBST(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function return check(root, INT_MIN, INT_MAX); }
相关文章推荐
- hive自定义mapred脚本运行机制分析
- Mac下使用OpenMP
- Amazon 开始接受 Windows 礼品卡预订
- jQuery 教程:简单的遮罩弹窗效果
- border-radius
- windows下使用命令启动服务
- JS 操作iframe
- MessageFormat妙用
- URL地址JS编码和解码
- java注解
- Java服务器非阻塞笔记
- JMeter 参数化、检查点、集合点
- ndisasm常用方法
- overflow属性在IE6下面失去效果
- 修改 上传图片按钮input-file样式。。
- fragment的实现与互相通信
- POJ 1704 Georgia and Bob (Nim游戏变形)
- 分布式系统架构的基本原则和实践
- 网络连接状态的监听和判断
- Treasure Hunt(poj1066线段相交)