美文网首页
98. Validate Binary Search Tree

98. Validate Binary Search Tree

作者: jecyhw | 来源:发表于2019-06-11 09:50 被阅读0次

题目链接

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

代码

class Solution {
public:
    bool isValidBST(TreeNode* root) {
        return dfs(root, NULL, NULL);
    }

    bool dfs(TreeNode* root, TreeNode* lower, TreeNode* upper) {
        if (root == NULL) {
            return true;
        }
        if (lower != NULL && root->val <= lower->val) {
            return false;
        }
        if (upper != NULL && root->val >= upper->val) {
            return false;
        }

        return dfs(root->left, lower, root) && dfs(root->right, root, upper);
    }
};

相关文章

网友评论

      本文标题:98. Validate Binary Search Tree

      本文链接:https://www.haomeiwen.com/subject/fhlffctx.html