-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/diguage/algorithm/leetcode/_0110_BalancedBinaryTree_2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/diguage/algorithm/leetcode/_0110_BalancedBinaryTree_21.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))); | ||
} | ||
} |