Validate binary search tree

关于这道题目,不得不感慨leetcode真的是一个不错的网站,之前的代码是有bug的,当时AC了,现在测试用例更加完善了,于是不能AC了。

题目描述:

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.

分析:1 题目中的BST的节点之间的值是一个严格递增的关系。先贴出今天的代码,很直观,就是中序遍历二叉树。查看数组是否是严格递增的即可。代码很简单:

class Solution {
private:
    void traverse(TreeNode *root , vector<int> &in)
    {
        if(root == nullptr) return;
        traverse(root->left,in);
        in.push_back(root->val);
        traverse(root->right,in);
    }

public:
    bool isValidBST(TreeNode *root) {
        if(root == nullptr) return true;
        vector<int> in;
        traverse(root,in);
        for(int i=0;i+1<in.size();++i)
            if(in[i] >= in[i+1]) return false;
        return true;
    }
};

这里重点解释一下之前的代码,先帖出代码:

class Solution { //代码不够robust!!!有错误!!
    bool helper(TreeNode *root, int min, int max)
    {
        if(root == nullptr) return true;
        return (root->val > min && root->val < max ) && helper(root->left,min,root->val) && helper(root->right,root->val, max);
    }

public:
    bool isValidBST(TreeNode *root) {
        if(root == nullptr) return true;
        int min = INT_MIN, max = INT_MAX;
        return helper(root, min,max);
    }
};

之前是逐渐的减少每个子树的取值范围,检验即可,直到根节点。这个代码对于一般的例子是没有任何问题的,但是对于树中本身就含有INT_MIN和INT_MAX的节点时,上述代码是错误的,列出几个case:

Case 1: 只有一个根节点,根节点的值就是INT_MIN或者INT_MAX.结果输出false,正确答案是TRUE。

于是我想到了对于特殊值的地方允许等号存在。代码极其繁琐,就不贴出来了,但是还是不对,比如下面的Case

Case 2: 根节点的值是INT_MIN ,根节点的右子节点为INT_MAX, 根节点的右子树的左子树节点的值为INT_MIN.

.......

时间: 2024-10-29 19:12:23

Validate binary search tree的相关文章

38: Validate Binary Search Tree

/************************************************************************/        /*       38:      Validate Binary Search Tree                            */        /************************************************************************/        /* 

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

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

【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 :: Validate Binary Search Tree[详细分析]

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 b

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

4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原理,请参见我之前的博客Validate Binary Search Tree 验证二叉搜索树.

39. Recover Binary Search Tree &amp;&amp; Validate Binary Search Tree

Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is prett

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: Validate Binary Search Tree [098]

[题目] 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