/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ publicbooleanisBalanced(TreeNode root){ //计算每个节点左右子树的深度,根据深度判断是否为平衡二叉树 if(root==null) returntrue; int left = depth(root.left); int right = depth(root.right); if(Math.abs(left-right)<=1) return isBalanced(root.left)&&isBalanced(root.right);//这里写的不好,还是递归了,后续改进 returnfalse;