您的位置:首页 > 其它

LeetCode基础--二叉树-判断二叉树是否平衡

2017-11-29 14:26 225 查看
题目描述:

求二叉树是否平衡,即:左右子树的高度差小于等于1,

实现:

public class Solution {
public bool IsBalanced(TreeNode root) {
if(root == null)
{
return true;
}
int L = Depth(root.left);
int R = Depth(root.right);
return Math.Abs(L-R) <= 1 && IsBalanced(root.left) && IsBalanced(root.right);
}
private int Depth(TreeNode root)
{
if(root == null)
{
return 0;
}
return Math.Max(Depth(root.left), Depth(root.right)) + 1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: