其他分享
首页 > 其他分享> > leetcode98.验证二叉树

leetcode98.验证二叉树

作者:互联网

题目链接

https://leetcode-cn.com/problems/validate-binary-search-tree/

个人题解

class Solution {
    bool isValidBST0(TreeNode* root,int &maxv,int &minv) {
        if (root->left == nullptr && root->right == nullptr) {
            maxv = root->val;
            minv = root->val;
            return true;
        }
        int lmaxv,lminv,rmaxv,rminv;
        if(root->left && !isValidBST0(root->left,lmaxv,lminv))return false;
        if(root->right && !isValidBST0(root->right,rmaxv,rminv))return false;
        if(root->left && lmaxv >= root->val) return false;
        if(root->right && rminv <= root->val) return false;
        if(root->right)maxv = rmaxv;else maxv = root->val;
        if(root->left) minv = lminv;else minv = root->val;
        return true;
    }
public:
    bool isValidBST(TreeNode* root) {
        int maxv,minv;
        return isValidBST0(root,maxv,minv);
    }
};

标签:leetcode98,right,return,val,验证,maxv,二叉树,root,minv
来源: https://www.cnblogs.com/Wade-/p/14253030.html