-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBinaryTree.java
More file actions
98 lines (89 loc) · 3.27 KB
/
BalancedBinaryTree.java
File metadata and controls
98 lines (89 loc) · 3.27 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* LeetCode problem 110. Balanced Binary Tree: https://leetcode.com/problems/balanced-binary-tree/
*/
public class Solution {
// instance var for isBalancedOptimised
// set to true, since maxDepth only changes to false
private boolean result = true;
/**
* Optimised way of determining whether the given tree is balanced
* Time Complexity: O(N), where N = the number of nodes in the given tree
* This version is faster than the non-optimised since the constant factor of N is smaller. This method calls
* maxDepth() which iterates through the entire tree only once. In the non-optimised version, a call is made at each
* level to a method which iterates through the tree from a given entry point. Thus, there will be many overlapping
* subproblems.
*
* @param root the root of the tree
* @return whether the given tree is balanced
*/
public boolean isBalancedOptimised(TreeNode root) {
maxDepth(root);
return result;
}
/**
* Finds the height of the given tree, changes instance variable result to false if there is height difference of
* more than one between the roots two child elements
*
* @param root the root of the tree
* @return the height of the tree
*/
public int maxDepth(TreeNode root) {
if (root == null) return 0;
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
if (Math.abs(leftHeight - rightHeight) > 1) {
result = false;
}
return 1 + Math.max(leftHeight, rightHeight);
}
/**
* Determines whether the given tree is balanced
* Time Complexity: O(N), where N = the number of nodes in the given tree
* <p>
* Space Complexity: O(H), where H = the height of the tree
* Since this uses recursion, at most there will be H calls to the method. Because Java re-uses space when making a
* new call, the maximum number of memory addresses on the stack at the same time is the tree height
*
* @param root the root of the tree
* @return whether the given tree is balanced
*/
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
// check if root is leaf
if (root.left == null && root.right == null) return true;
// if the height difference of its children is larger than 1, the tree is unbalanced
if (Math.abs(height(root.left) - height(root.right)) > 1) {
return false;
} else {
return isBalanced(root.left) && isBalanced(root.right);
}
}
/**
* Finds the height of the given tree
*
* @param root the root of the tree
* @return the height of the tree
*/
public int height(TreeNode root) {
if (root == null) {
return 0;
}
// the height of the tree is equal to 1 + the height of its child with the most elements
return 1 + Math.max(height(root.left), height(root.right));
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}