LeetCode --- Validate Binary Search Tree
题目链接
判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是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相关推荐
- LeetCode 98. Validate Binary Search Tree--C++解法--判断是否是BST--递归,迭代做法,中序遍历
LeetCode 98. Validate Binary Search Tree–C++解法–判断是否是BST–递归,迭代做法,中序遍历 LeetCode题解专栏:LeetCode题解 LeetCod ...
- [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 ...
- LeetCode 99. Recover Binary Search Tree
LeetCode 99. Recover Binary Search Tree 刚看到这个题真是一脸懵逼啊... 博客转载自:http://www.cnblogs.com/grandyang/p/42 ...
- LeetCode 之 JavaScript 解答第98题 —— 验证二叉搜索树(Validate Binary Search Tree)
Time:2019/4/24 Title: Vaildata Binary Search Tree Difficulty: Medium Author: 小鹿 题目:Vaildata Binary S ...
- LeetCode:Validate Binary Search Tree
题目链接 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 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 ...
- LeetCode OJ - Recover Binary Search Tree
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- LeetCode Validate Binary Search Tree(dfs)
问题:判断树是否是合法的二叉搜索树 思路: 1.在dfs时,判断当前结点是否大于左子树的最大值,小于右子树的最小值.这是一种思路 2.另外一个思路是在中序遍历时,用个变量记录当前结点的前驱结点,再判断 ...
- Leetcode: Validate Binary Search Tree
思路: 1. 难点在于构造递归函数的参数 2. 参数要包含上下界, 才能具有全局性. 第一次提交 WA 了, 因为写成来的判断函数是局部性的 代码: const int POS = 1E9; cons ...
最新文章
- Failed to load AppCompat ActionBar with unknown error
- java打印出继承体系的类(包括抽象类)、接口、域字段
- 【alibaba-cloud】sentinel的使用
- 程序出错后,程序员给测试人员的20条高频回复
- 基于visual graph开发实时线损管理系统
- fail2ban安全设置
- Image、Byte[]、Bitmap相互转换
- mysql8.0版本的服务器名称_MySQL 8.0安装部署-运维笔记
- Java 老矣,尚能饭否?2020 Java生态系统报告出炉
- 专科python应届生工资多少-Python最好的就业方向与就业岗位技能要求,能赚多少钱?...
- 测试工程师在软件测试阶段依据,北京软件测试工程师建立软件测试管理与评判体系...
- tcpdf使用及字体设置
- [ PyQt入门教程 ] Qt Designer工具的使用
- 初识组件--全局注册\局部注册
- HTML一个简单大一的网页作业
- ART-Pi系列文章
- centos7 安装Google Chrome浏览器
- JavaScript的内存管理
- 手机关机不拔电池也能被定位吗?
- 大三寒假实习了半个月很困惑