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

Appearance settings

Latest commit

 

History

History
History
28 lines (25 loc) · 698 Bytes

File metadata and controls

28 lines (25 loc) · 698 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Given a binary tree, determine if it is height-balanced.
*
* 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.
*
*/
public class BalancedBinaryTree {
public boolean isBalanced(TreeNode root) {
return determine(root) >= 0 ? true : false;
}
private int determine(TreeNode root) {
if (root == null) {
return 0;
} else {
int leftDepth = determine(root.left);
int rightDepth = determine(root.right);
if (leftDepth < 0 || rightDepth < 0
|| Math.abs(leftDepth - rightDepth) > 1)
return -1;
return Math.max(leftDepth, rightDepth) + 1;
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.