Ejemplo n.º 1
0
        private static SubtreeInfo Helper(LeetCode333TreeNode root)
        {
            if (root == null)
            {
                return(new SubtreeInfo());
            }

            var left  = Helper(root.left);
            var right = Helper(root.right);

            if (!left.valid || !right.valid || left.max >= root.val || right.min <= root.val)
            {
                left.valid = false;
                left.size  = Math.Max(left.size, right.size);
            }
            else
            {
                left.valid = true;
                left.size  = 1 + left.size + right.size;
            }

            left.min  = Math.Min(root.val, Math.Min(left.min, right.min));
            right.max = Math.Max(root.val, Math.Max(left.max, right.max));
            return(left);
        }
Ejemplo n.º 2
0
 public static int LargestBSTSubtree(LeetCode333TreeNode root)
 {
     return(Helper(root).size);
 }
Ejemplo n.º 3
0
 public LeetCode333TreeNode(int val = 0, LeetCode333TreeNode left = null, LeetCode333TreeNode right = null)
 {
     this.val   = val;
     this.left  = left;
     this.right = right;
 }