题目链接

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

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

附上代码:

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     // "fa" holds the last value that has been visited
13     // "flag" is false when it(the given binary tree or its subtree) is an invalid BST
14     void InOrder(TreeNode *root, int& fa, bool& flag) {
15         if (root->left != NULL) {
16             InOrder(root->left, fa, flag);
17         }
18         if (root->val <= fa)
19             flag = false;
20         fa = root->val;
21         if (root->right != NULL) {
22             InOrder(root->right, fa, flag);
23         }
24     }
25     bool isValidBST(TreeNode *root) {
26         if (root == NULL || root->left==NULL&&root->right==NULL) return true;
27         // initialize "fa" as INT_MIN
28         // I assume that there are no tree node's val equals to INT_MIN
29         // and it does... (test case like this doesnt exist)
30         int fa = INT_MIN;
31         bool flag = true;
32         InOrder(root, fa, flag);
33         if (flag)
34             return true;
35         else
36             return false;
37     }
38 };

转载于:https://www.cnblogs.com/Stomach-ache/p/3770071.html

LeetCode --- Validate Binary Search Tree相关推荐

  1. LeetCode 98. Validate Binary Search Tree--C++解法--判断是否是BST--递归,迭代做法,中序遍历

    LeetCode 98. Validate Binary Search Tree–C++解法–判断是否是BST–递归,迭代做法,中序遍历 LeetCode题解专栏:LeetCode题解 LeetCod ...

  2. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  3. LeetCode 99. Recover Binary Search Tree

    LeetCode 99. Recover Binary Search Tree 刚看到这个题真是一脸懵逼啊... 博客转载自:http://www.cnblogs.com/grandyang/p/42 ...

  4. LeetCode 之 JavaScript 解答第98题 —— 验证二叉搜索树(Validate Binary Search Tree)

    Time:2019/4/24 Title: Vaildata Binary Search Tree Difficulty: Medium Author: 小鹿 题目:Vaildata Binary S ...

  5. LeetCode:Validate Binary Search Tree

    题目链接 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  6. LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List--转换二叉树为双向链表--Java,C++,Python解法

    题目地址:Convert Binary Search Tree to Sorted Doubly Linked List - LeetCode Convert a BST to a sorted ci ...

  7. LeetCode OJ - Recover Binary Search Tree

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  8. LeetCode Validate Binary Search Tree(dfs)

    问题:判断树是否是合法的二叉搜索树 思路: 1.在dfs时,判断当前结点是否大于左子树的最大值,小于右子树的最小值.这是一种思路 2.另外一个思路是在中序遍历时,用个变量记录当前结点的前驱结点,再判断 ...

  9. Leetcode: Validate Binary Search Tree

    思路: 1. 难点在于构造递归函数的参数 2. 参数要包含上下界, 才能具有全局性. 第一次提交 WA 了, 因为写成来的判断函数是局部性的 代码: const int POS = 1E9; cons ...

最新文章

  1. Failed to load AppCompat ActionBar with unknown error
  2. java打印出继承体系的类(包括抽象类)、接口、域字段
  3. 【alibaba-cloud】sentinel的使用
  4. 程序出错后,程序员给测试人员的20条高频回复
  5. 基于visual graph开发实时线损管理系统
  6. fail2ban安全设置
  7. Image、Byte[]、Bitmap相互转换
  8. mysql8.0版本的服务器名称_MySQL 8.0安装部署-运维笔记
  9. Java 老矣,尚能饭否?2020 Java生态系统报告出炉
  10. 专科python应届生工资多少-Python最好的就业方向与就业岗位技能要求,能赚多少钱?...
  11. 测试工程师在软件测试阶段依据,北京软件测试工程师建立软件测试管理与评判体系...
  12. tcpdf使用及字体设置
  13. [ PyQt入门教程 ] Qt Designer工具的使用
  14. 初识组件--全局注册\局部注册
  15. HTML一个简单大一的网页作业
  16. ART-Pi系列文章
  17. centos7 安装Google Chrome浏览器
  18. JavaScript的内存管理
  19. 手机关机不拔电池也能被定位吗?
  20. 大三寒假实习了半个月很困惑

热门文章

  1. SpringBoot—分层结构
  2. 中间件的解析漏洞详解及演示
  3. Mybatis与Spring整合之配置文件方式
  4. Flutter入门:动画相关
  5. [RN] React Native 调试技巧
  6. 你不知道的JS(this)---#
  7. STM32 PWM输出(映射)
  8. 微信公众平台开发(97) 图文消息
  9. XML中预定义好的实体
  10. spring cloud: 使用consul来替换config server,config key/value 具体的配置详解