C# O(n) code


  • 0
    M
    public class Solution {
        int? previousValue = null;
        public bool IsValidBST(TreeNode root) {
            
            if (root == null  ) return true;
            bool leftOK = IsValidBST (root.left);
            if ((!leftOK || root.val <= previousValue) && previousValue!=null ) return false;
            previousValue = root.val;
            return IsValidBST (root.right);
        }
        
    }
    
    

Log in to reply
 

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