-
Notifications
You must be signed in to change notification settings - Fork 0
/
894.js
101 lines (92 loc) · 2.21 KB
/
894.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
/**
* 894. All Possible Full Binary Trees
* https://leetcode.com/problems/all-possible-full-binary-trees/
* Javascript solution
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number} N
* @return {TreeNode[]}
*/
const allPossibleFBT = function (N) {
let poss = [];
// No possible FBT with even number of nodes
if (N % 2 === 0) {
return poss;
}
if (memo[N]) {
return memo[N];
} else {
// For each odd combination adding up to N - 1, add combinations to memo
for (let i = 1; i < N - 1; i += 2) {
if (!memo[i]) {
memo[i] = allPossibleFBT(i);
}
}
// Generate combinations based on memo
let oddNodeCounts = getOddsLessThanOddN(N);
while (oddNodeCounts.length) {
let root = new TreeNode();
let front, back;
if (oddNodeCounts.length === 1) {
front = back = oddNodeCounts.pop();
} else {
front = oddNodeCounts.shift();
back = oddNodeCounts.pop();
}
memo[front].forEach(frontTreeNode => {
memo[back].forEach(backTreeNode => {
root.left = frontTreeNode;
root.right = backTreeNode;
poss.push(clone(root));
if (front !== back) {
root.right = frontTreeNode;
root.left = backTreeNode;
poss.push(clone(root));
}
});
});
}
memo[N] = poss;
return poss;
}
};
let memo = {
1: [new TreeNode()]
};
/**
* UTILS
*/
/**
* Clone a TreeNode to deal prevent mutation in allPossibleFBT
* @param {TreeNode} treeNode
* @returns {TreeNode}
*/
const clone = function (treeNode) {
let clonedTreeNode = new TreeNode();
if (treeNode.left) {
clonedTreeNode.left = clone(treeNode.left);
}
if (treeNode.right) {
clonedTreeNode.right = clone(treeNode.right);
}
return clonedTreeNode;
};
/**
* Return all odd numbers less than odd input N
* @param {Integer} N
*/
const getOddsLessThanOddN = function (N) {
let res = [];
for (let i = N - 2; i > 0; i -= 2) {
res.push(i);
}
return res;
};