using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return bool布尔型 */ public bool IsBalanced_Solution (TreeNode pRoot) { return IsBalanced2(pRoot) == -1 ? false:true; } public int IsBalanced2(TreeNode root){ if(root == null) return 0; int right = IsBalanced2(root.right); int left = IsBalanced2(root.left); if(left == -1 || right == -1 || Math.Abs(right - left) > 1) return -1; return (Math.Max(left, right) + 1); } }