Why this is wrong?


  • 0
    Y
    public boolean isValidBST(TreeNode root) {
        TreeNode pre = null;
        return helper(root, pre);
    }
    
    private boolean helper(TreeNode node, TreeNode pre) {
        if (node == null)
            return true;
        if (!helper(node.left, pre))
            return false;
        // visit current node
        if (pre != null && node.val <= pre.val) 
            return false;
        pre = node;
        return helper(node.right, pre);
    }
    

    It seems that pre is not updated when pass down, but I don't know why? Can someone help, thanks.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.