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 isValidBST(TreeNode* root) {
TreeNode * pPre = NULL;
TreeNode * pCur = root;
vector<TreeNode *> v; while(!v.empty() || NULL != pCur)
{
if(NULL != pCur)
{
v.push_back(pCur);
pCur = pCur->left;
}
else
{
if(pPre != NULL && v.back()->val <= pPre->val)
return false;
pPre = v.back();
v.pop_back();
pCur = pPre->right;
}
}
return true;
}
大神递归版:注意,每次左子树的值范围在最小值和根值之间,右子树的范围在根植和最大值之间。
public class Solution {
public boolean isValidBST(TreeNode root) {
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
} public boolean isValidBST(TreeNode root, long minVal, long maxVal) {
if (root == null) return true;
if (root.val >= maxVal || root.val <= minVal) return false;
return isValidBST(root.left, minVal, root.val) && isValidBST(root.right, root.val, maxVal);
}
}
【leetcode】Validate Binary Search Tree(middle)的更多相关文章
-
【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
-
【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
-
【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
-
【题解】【BST】【Leetcode】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
-
【leetcode】 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
-
【LeetCode】Validate Binary Search Tree 二叉查找树的推断
题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树 ...
-
Leetcode 之Validate Binary Search Tree(53)
判断是否是有效的二叉搜索树,即左子树的值小于根结点,右子树的值大于根结点.可以采用递归的方式来完成,递归时如何 传递有效的参数与根结点进行比较,是此题的难点. bool isValidBST(Tree ...
-
【Leetcode】【Medium】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
-
【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
随机推荐
-
NYOJ题目1047欧几里得
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAIcCAIAAACLpKQmAAAgAElEQVR4nO3dv1LjOsMH4O8m6LkQ6l ...
-
flume1.5.2安装与简介
关于flume的简介看参考:http://www.aboutyun.com/thread-7415-1-1.html 其实一张图就简单明了了 简单安装: 1.下载解压 ... 2.配置JDK,flum ...
-
svn: E180001: Unable to open an ra_local session to URL问题解决方案
在使用Android Studio的SVN导入项目时,出现了: svn: E180001: Unable to open an ra_local session to URLsvn: E180001: ...
-
html5 高级动画精灵
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
-
遇到looper之类关于消息循环的
原因大概是因为无法创建消息循环,这时候要考虑函数是否要在主线程或者不在主线程中进行,改一下即可
-
MQ消息队列配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
-
通过arcmap发布缓存服务,无法选择自定义方案
出现该问题是因为缓存目录有该缓存信息,清楚掉之后就可以选择自定义方案了
-
json-lib和dom4j实现JSON转XML
package com.geostar.gfstack.operationcenter.test; import net.sf.json.JSONObject; import net.sf.json. ...
-
Linux下输入某些命令时会提示:bash:command not found
首先,查看$PATH中是否包含了这些命令. $PATH:决定了shell到哪些目录中去寻找命令或程序,PATH值是一系列的目录.当运行程序时,linux到这些目录下搜索进行编译链接. 格式: PATH ...
-
记一次autofac+dapper+mvc的框架搭建实践
1,环境 .net framework4.7.2,Autofac,Autofac.Mvc5,sql server 2,动机 公司项目用的是ef,之前留下代码的大哥,到处using,代码没有分层,连复用 ...