LeetCode学习笔记(7) 第98题 Validate Binary Search Tree

题目

在这里插入图片描述

分析

1.假如root节点值为a, 那么左右子树上的值用一定有个范围, 利用范围求解
2.BST有个重要的性质, 就是采用中序遍历之后的序列一定是有序的。

流程

Solution 1 : 定义两个空指针分别表示无穷大或者无穷小的值,遍历左右子树时分别将root->val 设为最大,最小值
Solution 2 : 利用BST性质, 使用中序遍历, 定义一个成员变量记录pre_value, 比较与当前值得大小,大于等于返
			 回False,否则返回True。

代码

Solution 1:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        return isValidBST(root, nullptr, nullptr);
    }
private:
    bool isValidBST(TreeNode* root, int* minval, int* maxval){
        if(!root)
            return true;
        if((minval&& root->val <= *minval) || (maxval&& root->val >= *maxval))
            return false;
        return isValidBST(root->left, minval, &root->val) && isValidBST(root->right, &root->val, maxval);
        
        
        
    }
};

Solution 2:
soulution2.1(c++版):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<int> s;
        int b = 1;
        traversal(root, s, b);
        if(b == 0)
            return false;
        else
            return true;
    }
    void traversal(TreeNode* root, stack<int>& s, int& b){
        if(!root)
            return ;
        traversal(root->left,s, b);
        if(s.empty()) 
            s.push(root->val);
        else
            if(s.top() >= root->val)
            {
                b = 0;
                return;
            }
            else
            {
                s.push(root->val);
            }
        traversal(root->right, s, b);
        
    }
};

solution2.2(c++修改版):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        return inorder(root);
    }
private:
    TreeNode* pre;
    bool inorder(TreeNode* root){
        if(!root)
            return true;
        if(!inorder(root->left))
            return false;
        if(pre && root->val <= pre->val)
            return false;
        pre = root;
        return inorder(root->right);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值