From f8994be2d2f46972acfb986000630ddebe633a25 Mon Sep 17 00:00:00 2001 From: Joshua <47053655+Joshua-Lu@users.noreply.github.com> Date: Fri, 14 May 2021 00:51:13 +0800 Subject: [PATCH] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0110.平衡二叉树 Java版本,3种解法 --- problems/0110.平衡二叉树.md | 141 ++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index ac66cc40..11c31fc9 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -354,7 +354,146 @@ public: ## 其他语言版本 Java: +```Java +class Solution { + /** + * 递归法 + */ + public boolean isBalanced(TreeNode root) { + return getHeight(root) != -1; + } + private int getHeight(TreeNode root) { + if (root == null) { + return 0; + } + int leftHeight = getHeight(root.left); + if (leftHeight == -1) { + return -1; + } + int rightHeight = getHeight(root.right); + if (rightHeight == -1) { + return -1; + } + // 左右子树高度差大于1,return -1表示已经不是平衡树了 + if (Math.abs(leftHeight - rightHeight) > 1) { + return -1; + } + return Math.max(leftHeight, rightHeight) + 1; + } +} + +class Solution { + /** + * 迭代法,效率较低,计算高度时会重复遍历 + * 时间复杂度:O(n^2) + */ + public boolean isBalanced(TreeNode root) { + if (root == null) { + return true; + } + Stack stack = new Stack<>(); + TreeNode pre = null; + while (root!= null || !stack.isEmpty()) { + while (root != null) { + stack.push(root); + root = root.left; + } + TreeNode inNode = stack.peek(); + // 右结点为null或已经遍历过 + if (inNode.right == null || inNode.right == pre) { + // 比较左右子树的高度差,输出 + if (Math.abs(getHeight(inNode.left) - getHeight(inNode.right)) > 1) { + return false; + } + stack.pop(); + pre = inNode; + root = null;// 当前结点下,没有要遍历的结点了 + } else { + root = inNode.right;// 右结点还没遍历,遍历右结点 + } + } + return true; + } + + /** + * 层序遍历,求结点的高度 + */ + public int getHeight(TreeNode root) { + if (root == null) { + return 0; + } + Deque deque = new LinkedList<>(); + deque.offer(root); + int depth = 0; + while (!deque.isEmpty()) { + int size = deque.size(); + depth++; + for (int i = 0; i < size; i++) { + TreeNode poll = deque.poll(); + if (poll.left != null) { + deque.offer(poll.left); + } + if (poll.right != null) { + deque.offer(poll.right); + } + } + } + return depth; + } +} + +class Solution { + /** + * 优化迭代法,针对暴力迭代法的getHeight方法做优化,利用TreeNode.val来保存当前结点的高度,这样就不会有重复遍历 + * 获取高度算法时间复杂度可以降到O(1),总的时间复杂度降为O(n)。 + *

+ * 时间复杂度:O(n) + */ + public boolean isBalanced(TreeNode root) { + if (root == null) { + return true; + } + Stack stack = new Stack<>(); + TreeNode pre = null; + while (root != null || !stack.isEmpty()) { + while (root != null) { + stack.push(root); + root = root.left; + } + TreeNode inNode = stack.peek(); + // 右结点为null或已经遍历过 + if (inNode.right == null || inNode.right == pre) { + // 输出 + if (Math.abs(getHeight(inNode.left) - getHeight(inNode.right)) > 1) { + return false; + } + stack.pop(); + pre = inNode; + root = null;// 当前结点下,没有要遍历的结点了 + } else { + root = inNode.right;// 右结点还没遍历,遍历右结点 + } + } + return true; + } + + /** + * 求结点的高度 + */ + public int getHeight(TreeNode root) { + if (root == null) { + return 0; + } + int leftHeight = root.left != null ? root.left.val : 0; + int rightHeight = root.right != null ? root.right.val : 0; + int height = Math.max(leftHeight, rightHeight) + 1; + root.val = height;// 用TreeNode.val来保存当前结点的高度 + return height; + } +} +LeetCode题解链接:https://leetcode-cn.com/problems/balanced-binary-tree/solution/110-ping-heng-er-cha-shu-di-gui-fa-bao-l-yqr3/ +``` Python: @@ -401,4 +540,4 @@ func abs(a int)int{ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -

\ No newline at end of file +