LeetCode --- Validate Binary Search Tree
判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是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的更多相关文章
- 【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 ...
- 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 ...
- [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 ...
- 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 ...
- 【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 ...
- 【题解】【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 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 ...
- [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 ...
随机推荐
- HZOI2019熟练剖分(tree)
题目大意:https://www.cnblogs.com/Juve/articles/11186805.html 题解: 先给出官方题解: 其实这题跟期望没什么关系,因为E=$\sum_\limits ...
- Spring框架中的核心思想包括什么
(1)依赖注入 (2)控制反转 (3)面向切面
- Navicat12.1.7破解教程
https://blog.csdn.net/qq_39344689/article/details/85161342
- centos 重新编译php
说明:系统原来通过源码安装了php7.1.0.网上找了很多彻底删除原来php的办法,执行命令php -v PHP版本信息始终都在,说明方法都无用.自己大胆做了如下尝试,成功重新编译php 查找php ...
- gulp的安装以及less插件安装与使用
1.安装node.js 下载地址:http://nodejs.cn/download/ 这时我们输入 node -v 以及 npm -v 检查是否安装成功. 2.为了提高后续使用的快速,我们安装 ...
- c++容器中map的应用
原文链接:https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关 ...
- SecondaryNameNode 理解
NameNode将对文件系统的改动追加保存到本地文件系统上的一个日志文件(edits).当一个NameNode启动时,它首先从一个映像文件(fsimage)中读取HDFS的状态,接着应用日志文件中的e ...
- tp5 报 A non well formed numeric value encountered 的错解决办法
thinkphp5出现A non well formed numeric value encountered的解决办法修改formatDateTime方法如下 默认值: if (is_null($th ...
- java 中Vector的使用详解
Vector 可实现自动增长的对象数组. java.util.vector提供了向量类(vector)以实现类似动态数组的功能.在Java语言中没有指针的概念,但如果正确灵活地使用指针又确实可以大大提 ...
- 解决import javafx.geometry.Point2D无法导入的问题
windows->preferences->java->compiler->errors/warning->deprecated and restricted API-& ...