C# Solution, recursive and easy to understand.


  • 0
    D
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public bool IsValidBST(TreeNode root) {
            return IsValidBST(root, long.MinValue, long.MaxValue); 
        }
        
        public bool IsValidBST(TreeNode root, long min, long max){
            if (root == null){
                return true;
            }
            
            if (root.val <= min || root.val>=max){
                return false;
            }
    
            return IsValidBST(root.left, min, root.val) && IsValidBST(root.right, root.val, max);
        }
    }

Log in to reply
 

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