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 };

时间: 2024-08-16 13:06:01

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

【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 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

LeetCode OJ - Validate Binary Search Tree

题目: 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

[Leetcode][BST][Validate Binary Search Tree]

判断一颗树是不是二分查找树,非常经典基础的一个算法. 我很久之前第一次做的时候,是先求出来了树的前序遍历的结果,然后判断这个数组排序后是否和排序前相同,还要判断重复虾米的,很纠结的一种做法. 后来思考了一下怎么用递归的思路做,觉得应该根据定义返回两个子树的最大值和最小值,写了一会代码,发现好麻烦,不太对的样子. 后来看了题解,发现是用了一种反向的思维,把上下界从树的顶端传下去,而不是自下而上的约束.作者太机智了. 1 /** 2 * Definition for binary tree 3 *

[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 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

leetcode dfs Validate Binary Search Tree

Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions 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

leetcode 之 Validate Binary Search Tree

Validate Binary Search Tree 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 c

[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree

95. Validate Binary Search Tree/98. Validate Binary Search Tree 本题难度: Easy Topic: Binary Tree Description 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 contain

【LeetCode】Validate Binary Search Tree 解题报告

今天CSDN博客发生异常,折腾了大半天终于发出了这篇博文. [题目] 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

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 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