leetcode第一刷_Validate Binary Search Tree

有了上面的教训,这道题就简单多了,什么时候该更新pre是明白的了,倒是有个细节,二叉搜索树中是不同意有相等节点的,所以题目的要求用黑体字标明了。写的时候注意就能够了。

class Solution {
public:
    TreeNode *pre = NULL;
    bool isValidBST(TreeNode *root) {
        if(root == NULL)    return true;
        bool res = true;
        if(root->left)
            res &= isValidBST(root->left);
        if(pre&&root->val<=pre->val){
            return false;
        }
        pre = root;
        if(root->right)
            res &= isValidBST(root->right);
        return res;
    }
};


posted @ 2014-10-26 13:08  phlsheji  阅读(192)  评论(0编辑  收藏  举报