-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathunique-binary-search-trees.js
More file actions
34 lines (22 loc) · 936 Bytes
/
unique-binary-search-trees.js
File metadata and controls
34 lines (22 loc) · 936 Bytes
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
/**
* @param {number} n
* @return {number}
*/
var numTrees = function(n) {
// Create dp array
let dp = new Array(n+1).fill(0);
// Set base cases
dp[0] = 1;
dp[1] = 1;
// dp[i] represents each subtree (left and right) of the root node
for (let i = 2; i <= n; i++) {
for (let j = 0; j < i; j++) {
// dp[j] is the left side and dp[i-j-1] is the right side
// Example: n = 2
/* i = 2 & j = 0, there are 2 nodes, nodes on left of root = j = 0 and nodes on right of root = 2 - j (because we are using j nodes on the left) - 1 (1 to account for the root node itself), dp[i] += dp[0] * dp[1] = 1 * 1 = 1 */
// Final iteration dp[2] += dp[1] * dp[0] = 2
dp[i] += dp[j] * dp[i-j-1];
}
}
return dp[n];
}