题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
1 public class Solution { 2 boolean flag = true; 3 public boolean IsBalanced_Solution(TreeNode root) { 4 if(root == null) return true; 5 6 getDepth(root); 7 8 return flag; 9 } 10 11 public int getDepth(TreeNode root){ 12 if(root == null) return 0; 13 int left = getDepth(root.left); 14 int right = getDepth(root.right); 15 int len = left - right; 16 17 if(Math.abs(len) > 1) flag = false; 18 19 return left > right ? left + 1 : right + 1; 20 } 21 }