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 greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

这是easy题么=_= 被虐得好惨,开始想得简单,不就是验证BST咩,递归的话只用判断左右孩子是不是BST就行了。。。这就错了,孩子是BST并不能保证整颗树就是BST啊。比如下图

              5

              /    \

             3     6

             /   \  /  \

           1   8 4  7

5的左右孩子都分别是合法的BST,但是整颗树却不是,因为6的左孩子比5小;3的右孩子比5大。

所以在递归检测的时候,不仅仅要保证当前的树是BST,也要保证当前的树不破坏原有的BST结构,怎么办呢?

认真思考BST的结构性质会发现,BST就好像把一串数字做了很多切分,一部分放左变一部分放右边,一直这样递归的切下去。而这个切法可不是乱切,他是永远保持 “小的放左边,大的放右边” 的,这样切就有个性质,就是被切出来的每一段区域,都有严格的上下界。

              3

              /    \

             1     5

             /   \  /  \

           0   2 4  6

上图是一颗合法的BST,如果我们将其按照中序遍历方式展开可得:0 1 2 3 4 5 6

注意观察1,3,5 把序列切分成了4个区域,每个区域有一个数子分别是0,2,4,6。除了0没有下界,6没有上界以外,2和4都有明确的上下界。

所以在递归检测当中,我们应该同时传递上下界信息。

思路:在递归检测左右孩子的同时传递上下界信息,在递归调过程中更新上下界信息。

举个栗子:以上图为例,以根节点启动算法时,上下界信息为空,在检测1,3,5这个结构是否为合法BST后,递归的检测根节点为1的树(左孩子)并传入上界信息3;同样,递归检测根节点为5的树(右孩子)并传入下界信息3...

在递归过程中更新上下界信息,比如在检测到2这个节点的时候,更新了下界为1,而上界不用更新还是3。

bool validIter(TreeNode *node, TreeNode *left, TreeNode *right) {
if (!node) return true;
bool validLeft = true;
bool validRight = true;
if (node->left) {
validLeft = (node->val > node->left->val);
if (left) {
validLeft = validLeft && (node->left->val > left->val);
}
} if (node->right) {
validRight = (node->val < node->right->val);
if (right) {
validRight = validRight && (node->right->val < right->val);
}
} if (validLeft && validRight) {
return validIter(node->left, left, node) && validIter(node->right, node, right);
}
return false;
} bool isValidBST(TreeNode *root) {
return validIter(root, NULL, NULL);
}

要点:检测当前树是否为BST的时候还要检测是否违反了上下界的约束。

解法二:使用中序便利

上面的递归解法不太好想,我跌跌撞撞的提交了两次才对算法有了较深的理解。前面也说到,BST按照中序便利的话将会产生一个有序的序列,这是BST的性质。那为何不利用这个性质:先中序遍历产生数组,然后检查数组是否有序和是否有重复。

这个思路就是这么简单。。。中序遍历然后检查数组,所以才是easy题啊 (逃

void midOrderTraversal(TreeNode *node, vector<int> &s) {
if (!node) return;
midOrderTraversal(node->left, s);
s.push_back(node->val);
midOrderTraversal(node->right, s);
} bool isValidBST(TreeNode *root) {
if (!root) return true;
vector<int> s;
midOrderTraversal(root, s); for (int i = ; i < s.size(); i++) {
if (s[i] < s[i-] || s[i] == s[i-]) {
return false;
}
}
return true;
}

[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] Validate Binary Search Tree 验证二叉搜索树

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

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

  4. LeetCode: Validate Binary Search Tree [098]

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

  5. LeetCode :: Validate Binary Search Tree[具体分析]

    Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...

  6. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  8. 【LeetCode练习题】Validate Binary Search Tree

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

  9. 【leetcode】Validate Binary Search Tree

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

随机推荐

  1. ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  2. CentOS 6.5 安装Python 3.5

    1.CentOS6.5 安装Python 的依赖包 yum groupinstall "Development tools" yum install zlib-devel bzip ...

  3. poj3341

    AC自动机,用40^4 * 50 * 10的空间进行dp. 最大的难点在于hash. hash一个数列f,数列中的每一位都有一个上限g,即f[i]<=g[i]. 那么可以将该数列hash为这样一 ...

  4. NGUI Table页(UIToggle和UIToggledObjects)

    1.添加两个按钮Btn1和Btn2.添加2个Spr1和Spr2 2.给每个Btn添加两个脚本UIToggle和UIToggledObjects 3.将每个UIToggle的Group设置个非0的值 4 ...

  5. ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)

    当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...

  6. 手记-数学分析(高等数学)中有关算法效率的公式列举(O,Θ,Ω)

      权当数据结构与算法分析的学习手记     系数为一的幂级数部分和公式 ∑ n2 = 12 + 22 + 32 + ... + n2 = n(n+1)(2n+1)/6 = O(n3) ∑ n3 =  ...

  7. 【STL】next_permutation的原理和使用

    1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...

  8. js正则表达式替换空格

    str.replace(/^\s+|\s+$/g, '')   解析: str:要替换的字符串 \s : 表示 space ,空格+: 一个或多个^: 开始,^\s,以空格开始$: 结束,\s$,以空 ...

  9. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  10. Android Studio新建了一个项目看不到手机界面的效果

    我今天新建了一个项目,但是在这里却看不到手机的界面效果,如下图: