題目描述:
輸入一棵二叉樹,判斷該二叉樹是否是平衡二叉樹。
分析:
把這道題目放在二叉樹的深度一題后面,真的是讓人很開心啊,一下子就寫好了,創(chuàng)解題速度之最。
解答:
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) return true; //空樹也認(rèn)為是平衡的
//平衡樹的左右子樹高度差最大為1
return (Math.abs(getDepth(root.left)-getDepth(root.right))<=1)?true:false;
}
private static int getDepth(TreeNode node) {
return (node == null )? 0 : 1+Math.max(getDepth(node.left), getDepth(node.right));
}
}

運(yùn)行成功