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]的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  6. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  7. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  8. 【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) ...

  9. 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 ...

随机推荐

  1. Linux:安装OpenSSH-Server E:Package openssh-server has no installation candidate

    $sduo apt-get install openssh-server Reading package lists… Done Building dependency tree Reading st ...

  2. VMware下安装虚拟机Ubuntu14.04 Server设置桥接方式

    我本地的采用的上网方式的拨号上网,IP段是一公网下的通过路由设置的局域网,网段182.18.1.* 本地连接包含以下: 其中无线上网卡的.WMware桥接是自定义的局域网IP段:192.168.253 ...

  3. C++函数模板template

    一.  问题: 强类型语言要求我们为所有希望比较的类型都实现一个实例 int min(int a, int b) { return a < b ? a : b; } double min(dou ...

  4. 配置Hibernate二级缓存

    首先找到配置EHCahe二级缓存需要添加的jar包 hibernate-release-4.1.9.Final→lib→optional→ehcache→下的ehcache-core-2.4.3.ja ...

  5. 4Web Service中的几个重要术语

    4.1WSDL: web service definition language 直译:Webservice定义语言 1.对应一种类型的文件.wsdl 2.定义了webservice的服务端与客户端应 ...

  6. AJAX-----11iframe模拟ajax文件上传效果原理3

    如果直接给用户提示上传成功,那么如果用户上传的文件比较大点,那么等上半天都没反映,那么用户很有可能会刷新或者关了从来等... 那么会给我们服务器带来一定的影响,所以我们可以对这方面的用户体验度进行提升 ...

  7. HTML 5 的data-* 自定义属性

    HTML 5 增加了一项新功能是 自定义数据属性 ,也就是  data-* 自定义属性.在HTML5中我们可以使用以 data- 为前缀来设置我们需要的自定义属性,来进行一些数据的存放.当然高级浏览器 ...

  8. win7 APPCRASH问题解决!

    真是废了老劲了..什么清理插件,各种运行msconfig/启动都试了 问题:**.exe已停止工作 问题事件名称: APPCRASH 应用程序名: compute_image_mean.exe 应用程 ...

  9. 原生JS--COOKIE

    原生JS--COOKIE: COOKIE基础及应用:1.什么是COOKIE==>页面用来保存信息,比如:自动登录,记住用户名2.COOKIE的特性:  --同一个网站中,所有的页面共享同一套co ...

  10. LeetCode----202. Happy Number(Java)

    package isHappy202; /* * Write an algorithm to determine if a number is "happy". A happy n ...