-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path110. Balanced Binary Tree.js
122 lines (102 loc) · 2.59 KB
/
110. Balanced Binary Tree.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
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.
Example 1:
Given the following tree [3,9,20,null,null,15,7]:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* ReturnType class used for maintain multiple values for each level
*/
// 1. Define Return Type
class ReturnType {
constructor(isBalanced, maxDepth) {
this.isBalanced = isBalanced;
this.maxDepth = maxDepth;
}
}
/**
* @param {TreeNode} root
* @return {boolean}
*/
const isBalanced = function(root) {
// 3. Return Helper function
return helper(root).isBalanced;
};
// 2. Define Helper function
/**
*
* @param {TreeNode} node
* @return {ReturnType}
*/
const helper = function(node) {
// 4. Base Case
if (node === null) {
return new ReturnType(true, 0);
}
// 5. Recursive Case
// 5.1 Find left subtree
// left is ReturnType, not a TreeNode
let left = helper(node.left);
// 5.2 Find right subtree
// right is ReturnType, not a TreeNode
let right = helper(node.right);
// 5.3 Handle false cases
if (!left.isBalanced || !right.isBalanced) return new ReturnType(false, -1);
if (Math.abs(left.maxDepth - right.maxDepth) > 1) return new ReturnType(false, -1);
// 5.4 Handle true case
return new ReturnType(true, Math.max(left.maxDepth, right.maxDepth) + 1);
};
/**
* Leetcode Fundamental: 11/14 Update
* Failure:
* 1. Fail to use isbalanced property in Command
* 2. Fail to find the position to update maxHeight
* 3. Fail to use a recursion helper function
* 4. Fail to compare left subtree and right subtree and generate false
*
* Runtime: 68 ms
*/
const isBalanced = function(root) {
return helper(root).isBalanced;
};
const helper = (node) => {
if (node === null) return Command(true, 0);
let left = helper(node.left);
let right = helper(node.right);
// Handle false case
if (!left.isBalanced || !right.isBalanced) return Command(false, -1);
// Generate false case
if (Math.abs(left.maxHeight - right.maxHeight) > 1) return Command(false, -1);
// Handle true case
return Command(true, Math.max(left.maxHeight, right.maxHeight) + 1);
};
function Command(isBalanced, maxHeight) {
return {
isBalanced,
maxHeight
}
}