public static int TreeDepth(TreeNode treeNode){
int ld = 0;
int rd = 0;
if (treeNode.leftNode != null) {
ld = TreeDepth(treeNode.leftNode);
}
if (treeNode.rightNode != null) {
rd = TreeDepth(treeNode.rightNode);
}
return ld > rd ? (ld+1):(rd+1);
}
public static boolean IsVal(TreeNode treeNode) {
int ld = 0;
int rd = 0;
if (treeNode.leftNode != null) {
ld = TreeDepth(treeNode.leftNode);
System.out.println(ld);
}
else{
return true;
}
if (treeNode.rightNode != null) {
rd = TreeDepth(treeNode.rightNode);
System.out.println(rd);
}
else{
return true;
}
return (Math.abs(ld - rd) <= 1) && IsVal(treeNode.leftNode) && IsVal(treeNode.rightNode); }
JAVA二叉树平衡检测
最新推荐文章于 2024-04-19 17:24:39 发布