private static void IsBalancedImpl(TreeNode <T> node, SortedSet <int> leafDepths, int currentDepth) { currentDepth++; if (node.Left != null && node.Right != null) { leafDepths.Append(currentDepth); } if (node.Left != null) { IsBalancedImpl(node.Left, leafDepths, currentDepth); } else if (node.Right != null) { IsBalancedImpl(node.Right, leafDepths, currentDepth); } }