天天看點

Leetcode-标簽為Tree 110. Balanced Binary Tree

原題

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

題目分析

判斷二叉樹是否為平衡二叉樹。

代碼實作

public class BanlancedTree
    {
        public bool IsBalanced(TreeNode root)
        {
            if (root == null)
                return true;
            if (Math.Abs(height(root.left) - height(root.right)) > )
                return false;
            return IsBalanced(root.left) && IsBalanced(root.right);
        }

        private int height(TreeNode node)
        {
            if (node == null)
                return ;
            return  + Math.Max(height(node.left), height(node.right));
        }
    }