leetcode 98: Validate Binary Search Tree
2015-08-11 13:11
393 查看
The corner case here is that some nodes have the value of INT_MAX and INT_MIN, so the top root should be differentiated from other roots. In my code, I use the minNode=NULL and maxNode=NULL to solve it.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isValidBST(TreeNode* root) { if(!root) return true; return helper(root,NULL,NULL); } bool helper(TreeNode* root,TreeNode* minNode,TreeNode* maxNode) { if(!root) return true; if(minNode&&root->val<=minNode->val||maxNode&&root->val>=maxNode->val) return false; return helper(root->left,minNode,root)&&helper(root->right,root,maxNode); } };
相关文章推荐
- python批量下载图片
- POSIX 线程小结
- 好记性不如烂笔头85-spring3学习(6)-BeanFactory 于bean生命周期
- [LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)
- 延迟调用
- python细节
- 我想成为大牛,第一队,不要单打独斗
- 【JS】js获得下拉列表选中项的值和id
- NSMutableArray在removeAllObjects时崩溃
- Android监听网络变化
- HDU 1005 Number Sequence
- nginx+tomcat实现动静分离(转)
- How to create XML validator from XML schema?
- MySQL常用函数 一
- UI10_cell的自适应高度
- 基于Spring Boot,Security和JWB的REST接口的无状态认证
- spring多数据源的配置(转)
- 从1到n整数中1出现的次数——剑指offer面试题32
- Ogre 创建角色对象的边缘线效果
- 理解CSS中的BFC(块级可视化上下文)[译]