C# solution .. (not many of these around)


  • 0
    D

    similar idea implemented else where .. just thought to share for convenience ..

    public class Solution {
    public bool IsValidBST(TreeNode root)
    {
    long[] ret = ensure(root);
    return ret[0] == 1;
    }

    private long[] ensure(TreeNode root)
    {
        long[] ret = new long[]{1,Int64.MaxValue,Int64.MinValue};
        
        if(root != null)
        {
            long[] leftret = ensure(root.left);
            long[] rightret = ensure(root.right);
            if(leftret[0] == 0 || rightret[0] == 0)
            {
                ret[0] = 0;
                return ret;
            }
            else
            {
                ret[0] = (leftret[2] < root.val && rightret[1] > root.val) ? 1 : 0;
                ret[1] = Math.Min(leftret[1], root.val); ret[2] = Math.Max(rightret[2], root.val);
            }
        }
        
        return ret;
    }
    

    }


Log in to reply
 

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