题目:判断二叉树是否为平衡二叉树
思路:迭代,需要借助二叉树的高度,判断一个树是否为平衡二叉树,则判断其左子树和右子树是否为平衡二叉树
public static boolean isAVLRec(TreeNode root){ if(root == null) return true; if(Math.abs(getDepth(root.left)- getDepth(root.right))>1){ return false; } return isAVLRec(root.left)&& isAVLRec(root.right); }
public static int getDepth(TreeNode root){ if(root == null) return 0; if(root.left == null && root.right == null) return 1; return Math.max(getDepth(root.left), getDepth(root.right))+1; }