4.1---二叉树是否平衡(CC150)

//也就是把高度在递归过程中给一并算了。
public
class Balance { public static boolean checkBalance(TreeNode root, int[] dep){//java 里没有传地址 if(null == root){ dep[0] = 0; return true; } int[] leftDep = new int[1]; int[] rightDep = new int[1]; boolean leftBalance = checkBalance(root.left, leftDep); boolean rightBalance = checkBalance(root.right, rightDep); dep[0] = Math.max(leftDep[0], rightDep[0]) + 1; return leftBalance && rightBalance && (Math.abs(rightDep[0] - leftDep[0]) <=1); } public boolean isBalance(TreeNode root) { // write code here int[] dep = new int[1]; return checkBalance(root, dep); } }

 

posted @ 2015-12-22 14:22  创业-李春跃-增长黑客  阅读(166)  评论(0编辑  收藏  举报