题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
public class Solution { public boolean IsBalanced_Solution(TreeNode root) { int ret = isbalanced(root) ; if (ret < 0) { return false; } return true; } public int isbalanced(TreeNode root) { if (root == null) return 0; int lH = isbalanced(root.left); int rH = isbalanced(root.right); int h = Math.max(lH, rH) + 1; if (lH < 0 || rH < 0) { return -1; } if (Math.abs(lH - rH) > 1) { return -1; } return h; } }