题目链接

判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是BST

如果该二叉树是BST, 那么对其中序遍历,所得序列一定是单调递增的(不考虑有重复数值的情况)

附上代码:

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// "fa" holds the last value that has been visited
// "flag" is false when it(the given binary tree or its subtree) is an invalid BST
void InOrder(TreeNode *root, int& fa, bool& flag) {
if (root->left != NULL) {
InOrder(root->left, fa, flag);
}
if (root->val <= fa)
flag = false;
fa = root->val;
if (root->right != NULL) {
InOrder(root->right, fa, flag);
}
}
bool isValidBST(TreeNode *root) {
if (root == NULL || root->left==NULL&&root->right==NULL) return true;
// initialize "fa" as INT_MIN
// I assume that there are no tree node's val equals to INT_MIN
// and it does... (test case like this doesnt exist)
int fa = INT_MIN;
bool flag = true;
InOrder(root, fa, flag);
if (flag)
return true;
else
return false;
}
};

LeetCode --- Validate Binary Search Tree的更多相关文章

  1. 【leetcode】Validate Binary Search Tree

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

  2. leetcode dfs Validate Binary Search Tree

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

  3. Java for LeetCode 098 Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. Leetcode 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. 【题解】【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 ...

  8. leetcode 98 Validate Binary Search Tree ----- java

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. 全栈之路-微信小程序-架构总览

    第一阶段是用来学习小程序开发的,这个就相当于PC端的网站吧,只不过现在依靠微信强大的流量来将业务搬移到小程序中,对于企业来说,这是一种很好的发展方向,既减少了开发成本,又减少了推广成本,小程序是很被人 ...

  2. 关于set的unordered特性

    关于set排序无序的问题,原因是set使用哈希表做内存索引. 详细介绍可见: https://stackoverflow.com/questions/12165200/order-of-unorder ...

  3. qq音乐网站页面切换歌手分类时不刷新

    1.提交表单时会自动刷新页面(提交表单一般使用post方式提交) 2.动态加载数据时页面不会刷新,只是把页面中某个位置的内容替换掉想要的内容 3.一般在切换到不同的html页面时才会强制让你把页面刷新 ...

  4. Location protocol 属性

    Location protocol 属性 定义和用法 protocol 属性是一个可读可写的字符串,可设置或返回当前 URL 的协议. 语法 location.protocol <!DOCTYP ...

  5. Dom4j官网解释实例

    Dom4j是一个易于使用的,开源的库,在Java平台上与XML,XPath,XSLT协同工作.使用Java集合框架,全面支持DOM,SAX,JAXP. 官方网站:http://dom4j.org 1. ...

  6. jqGrid列的统计

    $("#List").jqGrid({ url: "${pageContext.request.contextPath}/cbfx/getCbhzList.do" ...

  7. Broken Keyboard UVA 11988 数组实现链表

    这个构造十分巧妙,,,又学到一招,有点类似数组实现的邻接表 #include <iostream> #include <string.h> #include <cstdi ...

  8. 本周汇总 动态rem适配移动端/块状元素居中/透明度

    1.动态rem适配移动端 !function(){ var width = document.documentElement.clientWidth; var head=document.getEle ...

  9. 20190817-T1-LOJ6322「雅礼国庆 2017 Day6」Star Way To Heaven

    写这篇题解是因为作者太蒻已经忘了最小生成树了. <题面> 这个题还真是想不到最小生成树. $80\%$算法 复杂度:$\Theta(k^2 \log N )$ 用了二分答案(明显答案具有单 ...

  10. poj 2115 扩展欧几里德

    #include<stdio.h> #include<string.h> #define max 32 typedef long long LL; LL pow2[max+]; ...