c# solution, O(1) space, recursion, 7 lines


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

Log in to reply
 

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