剑指Offer-平衡二叉树

题目描述#

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

思路#

思路一:

遍历每个结点,借助一个获取树深度的递归函数,根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。时间复杂度:$O(n^2)$

思路二:

从下往上遍历,如果子树是平衡二叉树,则返回子树高度,否则返回-1。时间复杂度:$O(n)$

代码实现#

Copy
package Tree; /** * 平衡二叉树 * 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 * 平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质: * 它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。 */ public class Solution20 { public static void main(String[] args) { Solution20 solution20 = new Solution20(); int[] array = {8, 6, 10, 5, 7, 9, 11}; TreeNode treeNode = solution20.createBinaryTreeByArray(array, 0); System.out.println(solution20.IsBalanced_Solution_2(treeNode)); } /** * 从下往上遍历,如果子树是平衡二叉树,则返回子树高度,否则返回-1 * 时间复杂度:O(n) * * @param root * @return */ public boolean IsBalanced_Solution_2(TreeNode root) { return MaxDepth_2(root) != -1; } public int MaxDepth_2(TreeNode root) { if (root == null) { return 0; } int leftHeight = MaxDepth_2(root.left); if (leftHeight == -1) { return -1; } int rightHeight = MaxDepth_2(root.right); if (rightHeight == -1) { return -1; } return Math.abs(leftHeight - rightHeight) > 1 ? -1 : 1 + Math.max(leftHeight, rightHeight); } /** * 遍历每个结点,借助一个获取树深度的递归函数,根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。 * 时间复杂度:O(n^2) * * @param root * @return */ public boolean IsBalanced_Solution(TreeNode root) { if (root == null) { return true; } if (Math.abs(MaxDepth(root.left) - MaxDepth(root.right)) > 1) return false; return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right); } public int MaxDepth(TreeNode root) { if (root == null) { return 0; } return 1 + Math.max(MaxDepth(root.left), MaxDepth(root.right)); } public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public TreeNode createBinaryTreeByArray(int[] array, int index) { TreeNode tn = null; if (index < array.length) { int value = array[index]; tn = new TreeNode(value); tn.left = createBinaryTreeByArray(array, 2 * index + 1); tn.right = createBinaryTreeByArray(array, 2 * index + 2); return tn; } return tn; } }
posted @   武培轩  阅读(352)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示
CONTENTS