forked from walnutown/CodingInTheDeep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBinaryTree.java
More file actions
53 lines (47 loc) · 1.95 KB
/
Copy pathBalancedBinaryTree.java
File metadata and controls
53 lines (47 loc) · 1.95 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
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.
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// At the first glance, it's easy to have the idea of recursively checking the left sub-tree and right sub-tree, plus the
// requirements that the diff between the height of left and right sub-tree is no more than one.
// that's:
// isBalanced(root.left) && isBalanced(root.right) && lHeight- rHeight <= 1
// in this case, we have to do a DFS on the subtree to get the height of the subtree, the total time complexity is O(n^2)
// Yet, actually, we can check whether the subtree is balanced when we recurse through the subtree. Then, we can reduce the
// big O to O(n), and the space complexity is O(H), where H is the height of the tree.
// Recursion
// time: O(n); space: O(h), h is the largest height of the tree
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root==null) return true;
return getHeight(root) > -1;
}
public int getHeight(TreeNode node){
if (node==null) return 0;
int l = getHeight(node.left), r = getHeight(node.right);
return Math.abs(l-r)>1 || l<0 || r<0 ? -1 : Math.max(l, r) + 1 ;
}
}
// time optimize the above solution
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root==null) return true;
return getHeight(root) > -1;
}
public int getHeight(TreeNode node){
if (node==null) return 0;
int l = getHeight(node.left);
if (l<0) return -1; // can skip checking of right child here
int r = getHeight(node.right);
return Math.abs(l-r)>1 || r<0 ? -1 : Math.max(l, r) + 1 ;
}
}