private static Nullable <int> BalancedHeightRecursive(BinaryTreeNode <int> node) { if (node == null) { return(-1); } Nullable <int> leftHeight = IsHeightBalanced.BalancedHeightRecursive(node.Left); if (leftHeight == null) { return(null); } Nullable <int> rightHeight = IsHeightBalanced.BalancedHeightRecursive(node.Right); if (rightHeight == null) { return(null); } if (Math.Abs(rightHeight.Value - leftHeight.Value) > 1) { return(null); } return(Math.Max(rightHeight.Value, leftHeight.Value) + 1); }
private static bool Recursive(BinaryTreeNode <int> root) { return(IsHeightBalanced.BalancedHeightRecursive(root) != null); }