题目描述
实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1。
给定指向树根结点的指针TreeNode* root,请返回一个bool,代表这棵树是否平衡。
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class Balance {
public boolean isBalance(Map<TreeNode,Integer> node2Depth, TreeNode root) {
// write code here
if(root==null)
return true;
if(Math.abs(deepth(node2Depth, root.left)-deepth(node2Depth, root.right))>1)
return false;
else
return isBalance(node2Depth, root.left) && isBalance(node2Depth, root.right);
}
public int deepth(TreeNode root){
if(root==null)
return 0;
if(node2Depth.containsKey(root)){
return node2Depth.get(root);
}else{
int depth = Math.max(deepth(root.left),deepth(root.right))+1;
node2Depth.put(root, depth);
return depth;
}
}
}