Leetcode-ラベルはTree 110である.Balanced Binary Tree
1668 ワード
原題
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)) > 1)
return false;
return IsBalanced(root.left) && IsBalanced(root.right);
}
private int height(TreeNode node)
{
if (node == null)
return 0;
return 1 + Math.Max(height(node.left), height(node.right));
}
}