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. python install (version problem-method ln -s)

    一般情况下,无需自己安装Python.因为在大多数Linux版本中,如Fedora,Ubuntu等,都已经默认安装了Python,但也可以根据需要自定义安装Python.下面使用源码编译安装来举个例子 ...

  2. DNS分别在什么情况下使用UDP和TCP

    DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.但很少有人知道DNS分别在什么情况下使用这两种协议.     如果用wiresha ...

  3. Spring框架简介 Spring Framework Introduction

    Introduction The Spring Framework provides a comprehensive programming and configuration model for m ...

  4. [原创]java WEB学习笔记100:Spring学习---Spring Bean配置:SpEL详细介绍及代码演示

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. js onmouseleave

    onmouseleave  no  propagation onmouseout   will  propagation

  6. 0-systemctl开机启动项

    防火墙:iptables Apache服务名称:httpd MySQL服务名称:mysqld VSFTP服务名称:vsftpd <!--CentOS7新指令--> 使某服务 自动启动 sy ...

  7. 列间距column-gap

    column-gap主要用来设置列与列之间的间距,其语法规则如下: column-gap: normal || <length> 取值说明 属性值 属性值说明 normal 默认值,默值为 ...

  8. (一) ARM 内存SDRAM 讲解

    2.SDRAM内存工作原理 上面产生的误解关于 Bank ,这个bank 不是 和 S3C2440 芯片有关系(RAM 自身有bank , SDRAM 自身也有bank ,就像书 有 好几章节一样) ...

  9. Android -- 使用ViewPager实现画廊效果

    1,今天在微信推送文章看到实现画廊效果,感觉挺不错的,就来写写试试,先来看一下效果图: 上面的效果基本上可以用两个功能点来包含:ViewPager的切换动画.ImageView的倒影的实现 嗯,先来将 ...

  10. mysql复制表结构及检查表、存储过程是否存在

    mysql命令行复制表结构的方法: 1.只复制表结构到新表 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2  或者 CREATE TABLE 新表 LIKE 旧表 ...