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 be binary search trees.

回顾一下BST的定义,任一节点的子孙分别递归满足,左子孙小于该节点,右子孙大于该节点;只要判断这个就OK了;

一个主意点:在递归程序里面,仅判断一个节点大于左儿子小于右儿子是不够的,这样对于左儿子的右儿子以及右儿子的左儿子的

错误判断不到,因此需要将节点值变为边界值,递归下传;

代码如下:

class Solution {
public:
    bool isValidBST(TreeNode *root) {
        return check(root, INT_MIN, INT_MAX);
    }

private:
    bool check(TreeNode *root, int left, int right){
        if(root == NULL)
            return true;
        return (root->val > left) && (root->val < right)
               && check(root->left, left, root->val) &&check(root->right, root->val, right);
    }//这里的左儿子的左界用上面传下来的,右界用节点值,右儿子镜面对称
};

PS:按照注意点提到的思路写的错误代码

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode *root) {
        if (root == NULL)
            return true;

        bool bleft = true, bright = true;
        if (root->left != NULL){
            if (root->val > root->left->val){    //注意这里检验以及递归是不能保证根节点大于左边所有的,矛盾在于,左儿子的右儿子,以及右儿子的左儿子;
                bleft = isValidBST(root->left);
            }
            else{
                return false;
            }
        }

        if (root->right != NULL){
            if (root->val < root->right->val){
                bright = isValidBST(root->right);
            }
            else{
                return false;
            }
        }
        return bleft && bright;
    }
};

LeetCode :: Validate Binary Search Tree[详细分析],布布扣,bubuko.com

时间: 2024-10-11 23:35:23

LeetCode :: Validate Binary Search Tree[详细分析]的相关文章

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

LeetCode:: Unique Binary Search Trees[详细分析]

Unique Binary Search Trees My Submissions Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 设S(n)为n个

[leetcode]Validate Binary Search Tree @ Python

原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首先想到需要进行递归来解决问题.这道题递归的比较巧妙.让我们来看下面一棵树: 4 /    \ 2 6 /    \   /   \  1      3 5    7 对于这棵树而言,怎样进行递归呢?root.left这棵树的所有节点值都小于root,root.right这棵树的所有节点值都大于roo

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 (检验是否为二叉查找树) Python

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

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 验证二叉搜索树

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 (两种解法)

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

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