private boolean isBalanced=true;
public boolean isBalanced(TreeNode root) {
if(root==null){
return true;
}
height(root);
return isBalanced;
}
private int height(TreeNode root){
if(root==null){
return 0;
}
int left=height(root.left);
int right=height(root.right);
if(Math.abs(left-right)>1){
isBalanced=false;
}
return 1+Math.max(left,right);
}
https://www.mianshi.online,https://www.i9code.cn
本文由博客一文多发平台 OpenWrite 发布!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。