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 ...
随机推荐
- PRML读书笔记——Mathematical notation
x, a vector, and all vectors are assumed to be column vectors. M, denote matrices. xT, a row vcetor, ...
- @Override的作用
@Override是伪代码,表示重写(不写也可以,但是有些IDE会报warning),不过写上有如下好处: 1.可以当注释用,方便阅读:2.编译器可以给你验证@Override下面的方法名是否是你父类 ...
- Anaconda安装与使用
bash Anaconda.....sh命令安装成功 安装位置:/home/amelie/anaconda2 修改路径:vi ~/.bashrc vi编辑器在最后写上:export PATH=/hom ...
- oracle表的操作简述
单表的操作!(代码完全可以用手工代替,写下来为了记忆)1.建立表create table HKB_TABLE_MODIFY( NAME VARCHAR2(6), AGE VARCHAR2(3), ...
- Retrieve失败解决办法一例
错误:The service '/XRMServices/2011/OrganizationData.svc' cannot be activated due to an exception duri ...
- CentOS7安装mysql5.6.26
linux系统CentOS7 到http://mirrors.sohu.com/mysql/下载想要的mysql版本 这里用到的是 mysql-5.6.26-linux-glibc2.5-x86_64 ...
- php实现回复图文,图片,文字
[代码]php代码: <?php /** 微信公众平台 开发者模式 默认用户输入任何文字,均返回同一个图文信息,链接地址为手机站; 可以根据变量$keyword,即用户输入的信息,进行判断, ...
- python操作TexturePacker批量打包资源plist png
import os,sys imagedir = 'D:\\PackerImg\\imgDir' outplistdir = 'D:\\PackerImg\\outDir' comend = 'Tex ...
- Visual Studio 2015简体中文企业版/专业版下载+有效激活密钥
Visual Studio 2015是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.所写的目标代码适用于微软支持的所有 ...
- 预编译命令 #if DEBUG
在控制台程序根据预编译命令: http://www.askapache.com/windows/advanced-batch-scripting.html namespace SXGYCarTrans ...