本文共 549 字,大约阅读时间需要 1 分钟。
JZ39
import java.lang.Math;public class Solution { public boolean IsBalanced_Solution(TreeNode root) { if(root == null) return true; // 判断左右两个子树的高度差 && 左右两个子树都是一棵平衡二叉树 return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <=1 && IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right); } // 获取左右两个子树的最大高度 public int maxDepth(TreeNode root) { if(root == null) return 0; return 1 + Math.max(maxDepth(root.left),maxDepth(root.right)); }}
转载地址:http://mune.baihongyu.com/