Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit66e640e

Browse files
refactor 110
1 parent61945af commit66e640e

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
importcom.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
publicclass_110 {
1137

12-
classSolution1 {
38+
publicstaticclassSolution1 {
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-
classSolution2 {
63+
publicstaticclassSolution2 {
3864

3965
publicbooleanisBalanced(TreeNoderoot) {
4066
returngetH(root) != -1;
@@ -59,4 +85,4 @@ private int getH(TreeNode root) {
5985
}
6086
}
6187

62-
}
88+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp