Esempio n. 1
0
        public bool IsBalanced()
        {
            int leftHeight  = 0;
            int rightHeight = 0;

            if (this.LeftNode != null)
            {
                leftHeight = this.LeftNode.Height();
            }

            if (this.RightNode != null)
            {
                rightHeight = this.RightNode.Height();
            }

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


            bool isLeftBalanced = LeftNode != null?LeftNode.IsBalanced() : true;

            bool isRightBalanced = RightNode != null?RightNode.IsBalanced() : true;

            return(isLeftBalanced && isRightBalanced);
        }
Esempio n. 2
0
        public bool IsBalanced()
        {
            int LeftHeight = LeftNode != null?LeftNode.Height() : 0;

            int RightHeight = RightNode != null?RightNode.Height() : 0;

            int heightDifference = LeftHeight - RightHeight;

            if (Math.Abs(heightDifference) > 1)
            {
                return(false);
            }
            else
            {
                return((LeftNode != null ? LeftNode.IsBalanced() : true) && (RightNode != null ? RightNode.IsBalanced() : true));
            }
        }