Skip to content

Commit

Permalink
再次完成110
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Jun 21, 2024
1 parent be571f8 commit 5c9daee
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/0000-21-dynamic-programming.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include::0000-21-dp-4-palindromic-subsequence.adoc[leveloffset=+1]
include::0000-21-dp-5-longest-common-substring.adoc[leveloffset=+1]


[#tree-dp-trick]
== 树形 DP 套路

树形 DP 套路使用前提:如果题目求解目标是 S 规则,则求解释流程可以定成以每一个节点为头节点的子树在 S 规则下的每一个答案,并且最终答案一定就在其中。
Expand Down
21 changes: 20 additions & 1 deletion docs/0110-balanced-binary-tree.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[#0110-balanced-binary-tree]
= 110. Balanced Binary Tree

https://leetcode.com/problems/balanced-binary-tree/[LeetCode - Balanced Binary Tree]
Expand Down Expand Up @@ -55,7 +56,7 @@ Return false.

== 参考资料

. https://leetcode-cn.com/problems/balanced-binary-tree/solution/balanced-binary-tree-di-gui-fang-fa-by-jin40789108/[Balanced Binary Tree (Top-down和Bottom-up递归方法) - 平衡二叉树 - 力扣(LeetCode)]
. https://leetcode.cn/problems/balanced-binary-tree/solutions/8737/balanced-binary-tree-di-gui-fang-fa-by-jin40789108/[Balanced Binary Tree (Top-down和Bottom-up递归方法) - 平衡二叉树 - 力扣(LeetCode)]


[[src-0110]]
Expand All @@ -64,3 +65,21 @@ Return false.
include::{sourcedir}/_0110_BalancedBinaryTree.java[]
----

== 基于树形 DP 套路解法

这个解法参考了:xref:0000-21-dynamic-programming.adoc[动态规划] 中介绍的 “树形 DP 套路” 解法。

[{java_src_attr}]
----
include::{sourcedir}/_0110_BalancedBinaryTree_2.java[]
----

== 剪枝优化后的解法

参考论坛中大家的讨论,可以在发现不平衡时,就及时返回,停止执行其他相关递归调用,起到“剪枝”的作用。同时,时间复杂度也可以做到最好。

[{java_src_attr}]
----
include::{sourcedir}/_0110_BalancedBinaryTree_21.java[]
----

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.diguage.algorithm.leetcode;

import com.diguage.algorithm.util.TreeNode;

import java.util.Objects;

import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
import static java.util.Arrays.asList;

/**
* = 110. Balanced Binary Tree
*
* https://leetcode.com/problems/balanced-binary-tree/[LeetCode - Balanced Binary Tree]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-02-06 23:10
*/
public class _0110_BalancedBinaryTree_2 {

/**
* 使用“树形 DP 套路”
*/
public boolean isBalanced(TreeNode root) {
return dfs(root).balance;
}

public Record dfs(TreeNode root) {
if (root == null) {
return new Record(true, 0);
}
Record left = dfs(root.left);
Record right = dfs(root.right);
boolean balance = left.balance && right.balance && Math.abs(left.height - right.height) <= 1;
return new Record(balance, Math.max(left.height, right.height) + 1);
}

public static class Record {
public boolean balance;
public int height;

public Record(boolean balance, int height) {
this.balance = balance;
this.height = height;
}
}

public static void main(String[] args) {
_0110_BalancedBinaryTree_2 solution = new _0110_BalancedBinaryTree_2();
System.out.println(!solution.isBalanced(buildTree(asList(1, 2, 2, 3, null, null, 3, 4, null, null, 4))));
System.out.println(solution.isBalanced(buildTree(asList(3, 9, 20, null, null, 15, 7))));
System.out.println(!solution.isBalanced(buildTree(asList(1, 2, 2, 3, 3, null, null, 4, 4))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.diguage.algorithm.leetcode;

import com.diguage.algorithm.util.TreeNode;

import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
import static java.util.Arrays.asList;

/**
* = 110. Balanced Binary Tree
*
* https://leetcode.com/problems/balanced-binary-tree/[LeetCode - Balanced Binary Tree]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-02-06 23:10
*/
public class _0110_BalancedBinaryTree_21 {

/**
* 看了论坛中大家的讨论,做了提前结束递归的优化。
*/
public boolean isBalanced(TreeNode root) {
return dfs(root) >= 0;
}

public int dfs(TreeNode root) {
if (root == null) {
return 0;
}
int left = dfs(root.left);
// 发现不平衡即提前结束递归
if (left < 0) {
return -1;
}
int right = dfs(root.right);
// 发现不平衡即提前结束递归
if (right < 0) {
return -1;
}
return Math.abs(left - right) > 1 ? -1 : Math.max(left, right) + 1;
}

public static void main(String[] args) {
_0110_BalancedBinaryTree_21 solution = new _0110_BalancedBinaryTree_21();
System.out.println(!solution.isBalanced(buildTree(asList(1, 2, 2, 3, null, null, 3, 4, null, null, 4))));
System.out.println(solution.isBalanced(buildTree(asList(3, 9, 20, null, null, 15, 7))));
System.out.println(!solution.isBalanced(buildTree(asList(1, 2, 2, 3, 3, null, null, 4, 4))));
}
}

0 comments on commit 5c9daee

Please sign in to comment.