題目描述
輸入一棵二叉樹,判斷該二叉樹是否是平衡二叉樹,
牛客網鏈接
java代碼
import java.lang.Math;
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if (root == null) return true;
return getDeepth(root) != -1;
}
private int getDeepth(TreeNode root) {
if (root == null) return 0;
int left = getDeepth(root.left);
if (left == -1) return -1;
int right = getDeepth(root.right);
if (right == -1) return -1;
if (Math.abs(left-right) > 1) return -1;
return Math.max(left, right) + 1;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/125568.html
標籤:其他
