示例#1
0
文件: TreeNode.cs 项目: mturaga/bst
        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);
        }
示例#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));
            }
        }