I am not sure why my code does not pass the simple test


  • 0
    X

    I used in-order traversal to validate BST. But it even failed a simple test like {0,-1}. However, I used my own compiler and saw my code passed the test case. I am confused. Anyone can help?

    public class Solution {
        private static int last = 0;
        private static boolean isSet = false;
        public boolean isValidBST(TreeNode root) {
            return traversalBST(root);
        }
        
        private boolean traversalBST(TreeNode root) {
            if (root == null) {
                return true;
            }
            
            if(!traversalBST(root.left)) {
                return false;
            }
            int current = root.val;
            if (isSet && current <= last) {
                return false;
            }
            last = current;
            isSet = true;
            return traversalBST(root.right);
        }
    }

  • 0

  • 0
    C

    You must delete the 'static' key word, because 'static' indicates that the variable's life cycle will exist in all class objects.


Log in to reply
 

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