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 ...
随机推荐
- Hadoop学习(5)-- Hadoop2
在Hadoop1(版本<=0.22)中,由于NameNode和JobTracker存在单点中,这制约了hadoop的发展,当集群规模超过2000台时,NameNode和JobTracker已经不 ...
- js web实现移动端触控
// 触摸事件 $(".m_l_i_l a").on("touchstart", function(){ $(this).css("color&quo ...
- SQL Server错误与事务处理
T-SQL中出现的错误,依据和事务的关系,可以分为两种情况: 有的错误会导致发生错误位置之后的代码不再执行,如果错误位置在事务中,该事务也会自动回滚(即在错误位置之后的rollback语句不会执行,但 ...
- PHP的serialize序列化数据与JSON格式化数据
serialize序列化 我们在一些老的WEB系统中可能会看到在数据库或在文本文件中存储着一大串貌似有着特殊含义的字符串文本内容.我们仔细看会发现它具有数据类型和结构等信息,但是它并不容易人工阅读,它 ...
- Java 之 List<T> 接口的实现:LinkedList
Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap 隶属于队列Lis ...
- WinForm中跨线程操作控件
在WinForm编程时会遇到通过后台线程操作界面的情况,直接在后台线程执行的方法中直接操作控件会报错,这时候就要使用跨线程方式间接操作控件.下面是两种实现方式. 1.采用定义delegate的方式 ...
- :radio :checkbox
匹配所有单选按钮 示例 描述: 查找所有单选按钮 HTML 代码: <form> <input type="text" /> <input typ ...
- s3c2440 移值u-boot-2016.03 第5篇 支持dm9000 识别
1, 通过查看 /drivers/net/Makefile 发现想要编译上,需要添加宏 /include/configs/smdk2440.h 中添加 #define CONFIG_DRIVER_DM ...
- MAC: Homebrew(代替yum)安装
安装 ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" 最新方式请 ...
- dotfiles管理
刚刚知道dotfiles这个东西,百度也没发现什么太有价值的讲解,还都是英文,所以自己立志来好好屡屡清楚 1.dotfiles是什么?我自己的理解:linux下(mac下)有各种app,每个人会根据自 ...