public static boolean test5(BinTreeNode node) {
boolean[] res = {true};
getHeight(node, 1, res);
return res[0];
}
public static int getHeight(BinTreeNode node,int level,boolean[] res) {
if (node==null) {
return level;
}
int lh = getHeight(node.left, level+1, res);
if (!res[0]) {
return level;
}
int rh = getHeight(node.right, level+1, res);
if (!res[0]) {
return level;
}
if (Math.abs(lh-rh)>1) {
res[0]=false;
}
return Math.max(lh, rh);
}
Tree:判斷二叉樹是否為平衡二叉樹
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 題目:輸入一棵二叉樹的根結點,判斷該樹是不是平衡二叉樹。如果某二叉樹中任意結點的左右子樹的深度相差不超過1,那么它...