Skip to content

Commit 66e640e

Browse files
refactor 110
1 parent 61945af commit 66e640e

File tree

1 file changed

+30
-4
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-4
lines changed

src/main/java/com/fishercoder/solutions/_110.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,39 @@
33
import com.fishercoder.common.classes.TreeNode;
44

55
/**
6+
* 110. Balanced Binary Tree
7+
*
68
* Given a binary tree, determine if it is height-balanced.
9+
* For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
710
8-
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.*/
11+
Example 1:
12+
Given the following tree [3,9,20,null,null,15,7]:
13+
14+
3
15+
/ \
16+
9 20
17+
/ \
18+
15 7
19+
20+
Return true.
21+
22+
Example 2:
23+
Given the following tree [1,2,2,3,3,null,null,4,4]:
24+
25+
1
26+
/ \
27+
2 2
28+
/ \
29+
3 3
30+
/ \
31+
4 4
32+
33+
Return false.
34+
*/
935

1036
public class _110 {
1137

12-
class Solution1 {
38+
public static class Solution1 {
1339
//recursively get the height of each subtree of each node, compare their difference, if greater than 1, then return false
1440
//although this is working, but it's not efficient, since it repeatedly computes the heights of each node every time
1541
//Its time complexity is O(n^2).
@@ -34,7 +60,7 @@ private int getH(TreeNode root) {
3460
}
3561
}
3662

37-
class Solution2 {
63+
public static class Solution2 {
3864

3965
public boolean isBalanced(TreeNode root) {
4066
return getH(root) != -1;
@@ -59,4 +85,4 @@ private int getH(TreeNode root) {
5985
}
6086
}
6187

62-
}
88+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy