import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) return true;
int leftHeight = getTreeHeight(root.left);
int rightHeight = getTreeHeight(root.right);
if(leftHeight - rightHeight >1 || leftHeight - rightHeight <-1)
return false;
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int getTreeHeight(TreeNode root) {
if(root == null) return 0;
int leftLen = getTreeHeight(root.left);
int rightLen = getTreeHeight(root.right);
return leftLen > rightLen ? leftLen + 1 : rightLen + 1;
}
public static void main(String[] args) {
}
}
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
最新推荐文章于 2021-12-29 21:41:16 发布