Exemplo n.º 1
0
        private static bool BalancedHeightBruteForce(BinaryTreeNode <int> node)
        {
            if (node == null)
            {
                return(true);
            }

            if (!IsHeightBalanced.BalancedHeightBruteForce(node.Left))
            {
                return(false);
            }

            if (!IsHeightBalanced.BalancedHeightBruteForce(node.Right))
            {
                return(false);
            }

            int leftHeight  = node.Left == null ? -1 : node.Left.Data;
            int rightHeight = node.Right == null ? -1 : node.Right.Data;

            if (Math.Abs(leftHeight - rightHeight) > 1)
            {
                return(false);
            }

            node.Data = Math.Max(leftHeight, rightHeight) + 1;

            return(true);
        }
Exemplo n.º 2
0
 private static bool BruteForce(BinaryTreeNode <int> root)
 {
     return(IsHeightBalanced.BalancedHeightBruteForce(root));
 }