Validate Binary Search Tree [LeetCode]
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.
bool varifyBST(TreeNode * root, int * out_min, int * out_max) {
if(root == NULL)
return true;
(*out_min) = root->val;
(*out_max) = root->val;
if(root->left == NULL && root->right == NULL)
return true;
int vmax = root->val;
int vmin = root->val;
if(root->left != NULL){
bool ret = varifyBST(root->left, &vmin, &vmax);
if(ret == false)
return false;
if(vmax >= root->val)
return false;
(*out_min) = min(vmin, root->val);
}
if(root->right != NULL){
bool ret = varifyBST(root->right, &vmin, &vmax);
if(ret == false)
return false;
if(vmin <= root->val)
return false;
(*out_max) = max(vmax, root->val);
}
return true;
}
bool isValidBST(TreeNode *root) {
int vmax = ;
int vmin = ;
return varifyBST(root, &vmin, &vmax);
}
Validate Binary Search Tree [LeetCode]的更多相关文章
- Validate Binary Search Tree——LeetCode
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Validate Binary Search Tree leetcode java
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 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) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
随机推荐
- UE4中使用数据表(Data Table)
本文依据官方文档数据驱动游戏性元素整理而来. 做过游戏的应该都清楚,如果游戏稍微有点规模,那么使用数据驱动来做游戏一般是必不可少的一步,一般也就是策划通过本表的方式来解决.下面我们来简单说一下UE4中 ...
- JS倒计时——天时分秒
HTML代码: <div id="times_wrap" class="time_num"> 距离结束时间: <div cl ...
- 如何使用Android Studio开发/调试Android源码
本文是以源码中development/tools/idegen/README作为指导文档. 环境: Ubuntu 14.10,openJdk 1.7,Android Studio 1.0.2,andr ...
- em
macro jumptocaller(){ JumpToLocation(GetSymbolLocation((GetCurSymbol ())))}
- 滑动的Button
在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { publi ...
- printk函数日志级别的设置【转】
本文转载自: 下面执行cat /proc/sys/kernel/printk 打印出的四个数字分别代表: 控制台日志级别.默认的消息日志级别.最低的控制台日志级别和默认的控制台日志级别 只有当prin ...
- Centos6.7安装docker1.7.1
Docker当前发布的最新版本已经到了1.11,其官网上针对Centos的的安装需求如下: Docker requires a -bit installation regardless of your ...
- String类型,Function类型
1.String类型: 1)创建String对象: var str=new String(s); String(s); 参数:参数 s 是要存储在 String 对象中的值或转换成 ...
- 20160308001 GridView的Sorting排序
参考地址: http://www.cnblogs.com/yinluhui0229/archive/2011/08/01/2124169.html 功能介绍:单击gridview的某一列列头,可以对该 ...
- 【20160924】GOCVHelper 图像处理部分(2)
//根据轮廓的面积大小进行选择 vector<VP> selectShapeArea(Mat src,Mat& draw,vector<VP> contour ...