forked from soulmachine/algorithm-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced-binary-tree.java
More file actions
22 lines (18 loc) · 668 Bytes
/
balanced-binary-tree.java
File metadata and controls
22 lines (18 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Balanced Binary Tree
// 时间复杂度O(n),空间复杂度O(logn)
public class Solution {
public boolean isBalanced (TreeNode root) {
return balancedHeight (root) >= 0;
}
/**
* Returns the height of `root` if `root` is a balanced tree,
* otherwise, returns `-1`.
*/
private static int balancedHeight (TreeNode root) {
if (root == null) return 0; // 终止条件
int left = balancedHeight (root.left);
int right = balancedHeight (root.right);
if (left < 0 || right < 0 || Math.abs(left - right) > 1) return -1; // 剪枝
return Math.max(left, right) + 1; // 三方合并
}
}