平衡二叉树 发表于 2020-06-01 分类于 剑指Offer 本文字数: 522 阅读时长 ≈ 1 分钟 题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。 在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树 解答还有优化空间 12345678910111213141516171819public boolean IsBalanced_Solution(TreeNode root) { if (root == null) { return true; } int left = deepth(root.left) + 1; int right = deepth(root.right) + 1; return Math.abs(left - right) <= 1 && IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);}private int deepth(TreeNode root) { if (root == null) { return 0; } int left = deepth(root.left) + 1; int right = deepth(root.right) + 1; return Math.max(left, right);}