Skip to content

Commit 957122c

Browse files
committed
2 parents f198219 + f7b6a17 commit 957122c

12 files changed

+376
-3
lines changed

Easy/102-binaryTreeLevelOrder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var levelOrder = function(root) {
7272

7373
var helper = function(results, node, level) {
7474
if (!node) return results;
75-
if (level >= results.length) {
75+
if (level === results.length) {
7676
results[level] = [];
7777
}
7878
results[level].push(node.val);

Hard/117-populatingNextRightII.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* function TreeLinkNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {TreeLinkNode} root
11+
* @return {void} Do not return anything, modify tree in-place instead.
12+
*/
13+
// BFS, level order Traversal, set a dummy head at the beginnig of each level.
14+
// Is it O(1) space?
15+
var connect = function(root) {
16+
while (root) {
17+
var leftDummy = new TreeLinkNode(0);
18+
var currChild = leftDummy;
19+
while (root) {
20+
if (root.left) {
21+
currChild.next = root.left;
22+
currChild = currChild.next;
23+
}
24+
if (root.right) {
25+
currChild.next = root.right;
26+
currChild = currChild.next;
27+
}
28+
root = root.next;
29+
}
30+
// reset head to the left of each level.
31+
root = leftDummy.next;
32+
}
33+
};
34+
35+
36+
/**
37+
* Definition for binary tree with next pointer.
38+
* function TreeLinkNode(val) {
39+
* this.val = val;
40+
* this.left = this.right = this.next = null;
41+
* }
42+
*/
43+
44+
// BFS too, but constant space
45+
var connect = function(root) {
46+
// next level's head (beginnig)
47+
var head = root;
48+
// next level's last visited node
49+
var prev;
50+
// curr level's currently visiting node
51+
var curr;
52+
while (head) {
53+
curr = head;
54+
prev = null;
55+
head = null;
56+
while (curr) {
57+
if (curr.left) {
58+
if (prev) prev.next = curr.left;
59+
else head = curr.left;
60+
prev = curr.left;
61+
}
62+
if (curr.right) {
63+
if (prev) prev.next = curr.right;
64+
else head = curr.right;
65+
prev = curr.right;
66+
}
67+
curr = curr.next;
68+
}
69+
}
70+
};
71+
72+
// doesn't work, wrong answer. e.g. {1,2,3,4,5,#,6,7,#,#,#,#,8},
73+
// the common parent is one more level up
74+
var connect = function(root) {
75+
if (!root) return;
76+
77+
while (root) {
78+
var pNode = root;
79+
while (pNode) {
80+
var child = null;
81+
if (pNode.left && pNode.right) {
82+
pNode.left.next = pNode.right;
83+
child = pNode.right;
84+
} else {
85+
if (pNode.left) child = pNode.left;
86+
if (pNode.right) child = pNode.right;
87+
}
88+
if (child) {
89+
if (pNode.next) {
90+
if (pNode.next.left) child.next = pNode.next.left;
91+
else child.next = pNode.next.right;
92+
}
93+
}
94+
pNode = pNode.next;
95+
}
96+
97+
while (root && !root.left && !root.right) {
98+
root = root.next;
99+
}
100+
if (!root) break;
101+
if (root.left) root = root.left;
102+
else if (root.right) root = root.right;
103+
}
104+
};

Hard/145-binaryTreePostorder.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[]}
11+
*/
12+
// looks like there is a better way to implement
13+
var postorderTraversal = function(root) {
14+
var result = [];
15+
if (!root) return result;
16+
var stack = [root];
17+
var preVisitedNode = null;
18+
19+
while (stack.length > 0) {
20+
node = stack[stack.length - 1];
21+
if (!node.left && !node.right ||
22+
(preVisitedNode && (preVisitedNode === node.right || preVisitedNode === node.left))) {
23+
result.push(node.val);
24+
preVisitedNode = stack.pop();
25+
} else {
26+
if (node.right) {
27+
stack.push(node.right);
28+
}
29+
if (node.left) {
30+
stack.push(node.left);
31+
}
32+
}
33+
}
34+
35+
return result;
36+
};
37+
38+
// an improved version
39+
var postorderTraversal = function(root) {
40+
var result = [];
41+
if (!root) return result;
42+
var stack = [root];
43+
44+
while (stack.length > 0) {
45+
var node = stack[stack.length - 1];
46+
if (!node.left && !node.right) {
47+
result.push(node.val);
48+
node = stack.pop();
49+
} else {
50+
if (node.right) {
51+
stack.push(node.right);
52+
node.right = null;
53+
}
54+
if (node.left) {
55+
stack.push(node.left);
56+
node.left = null;
57+
}
58+
}
59+
}
60+
61+
return result;
62+
};

Medium/116-PopulatingNextRightPointersinEachNode.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Key: if a node is a parent's left child, then this node's next is its parent's right child node.
1111
* if a node is a parent node's right child, then this node's next is its parent's next node's left node.
12-
* (the parent node should have next child). The trick part is how to write code.
12+
* (the parent node should have next child). The trick part is how to write code.
1313
*
1414
* @param {TreeLinkNode} root
1515
* @return {void} Do not return anything, modify tree in-place instead.
@@ -27,3 +27,24 @@ var connect = function(root) {
2727
root = root.left;
2828
}
2929
};
30+
31+
// second try, slow, but easy understanding
32+
// the space is not constant
33+
var connect = function(root) {
34+
if (!root) return;
35+
root.next = null;
36+
var queue = [root];
37+
while (queue.length > 0) {
38+
var node = queue.shift();
39+
if (node.left) {
40+
node.left.next = node.right;
41+
queue.push(node.left);
42+
}
43+
if (node.right) {
44+
if (node.next) {
45+
node.right.next = node.next.left;
46+
}
47+
queue.push(node.right);
48+
}
49+
}
50+
};

Medium/139-wordBreak.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @return {boolean}
77
*/
88

9-
// recursion, not accepted, time exceeds limits.
9+
// recursion, not accepted, time exceeds limits. O(N^2)
1010
var wordBreak = function(s, wordDict) {
1111
return helper(s, wordDict, 0);
1212
};
@@ -22,3 +22,19 @@ var helper = function(s, wordDict, start) {
2222
}
2323
return false;
2424
};
25+
26+
// Dynamic, accepted
27+
var wordBreak = function(s, wordDict) {
28+
var canBreak = [true];
29+
for (var i = 0; i < s.length; i++) {
30+
if (!canBreak[i]) continue;
31+
for (var word of wordDict) {
32+
var wLength = word.length;
33+
if (canBreak[i + wLength]) continue;
34+
if (s.substring(i, i + wLength) === word) {
35+
canBreak[i + wLength] = true;
36+
}
37+
}
38+
}
39+
return canBreak[s.length] ? true : false;
40+
};

Medium/144-binaryTreePreorder.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,24 @@ var preorderTraversal = function(root) {
2424
}
2525
return order;
2626
};
27+
28+
// this is a more straightforward method, but slower than first one.
29+
// stack tracks the node visit order
30+
var preorderTraversal = function(root) {
31+
if (!root) return [];
32+
var result = [];
33+
var stack = [root];
34+
35+
while (stack.length > 0) {
36+
var node = stack.pop();
37+
result.push(node.val);
38+
if (node.right) {
39+
stack.push(node.right);
40+
}
41+
if (node.left) {
42+
stack.push(node.left);
43+
}
44+
}
45+
46+
return result;
47+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for binary tree
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @constructor
11+
* @param {TreeNode} root - root of the binary search tree
12+
*/
13+
// Is there a better solution?
14+
var BSTIterator = function(root) {
15+
this.stack = [];
16+
this.pushStack(root);
17+
};
18+
19+
/**
20+
* @this BSTIterator
21+
* @returns {boolean} - whether we have a next smallest number
22+
*/
23+
BSTIterator.prototype.hasNext = function() {
24+
return this.stack.length > 0 ? true : false;
25+
};
26+
27+
/**
28+
* @this BSTIterator
29+
* @returns {number} - the next smallest number
30+
*/
31+
BSTIterator.prototype.next = function() {
32+
var small = this.stack.pop();
33+
if (small.right) {
34+
this.pushStack(small.right);
35+
}
36+
return small.val;
37+
};
38+
39+
BSTIterator.prototype.pushStack = function(node) {
40+
while (node) {
41+
this.stack.push(node);
42+
node = node.left;
43+
}
44+
};
45+
46+
/**
47+
* Your BSTIterator will be called like this:
48+
* var i = new BSTIterator(root), a = [];
49+
* while (i.hasNext()) a.push(i.next());
50+
*/

Medium/179-largestNumber.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {string}
4+
*/
5+
var largestNumber = function(nums) {
6+
nums.sort(function(a, b) {
7+
var ab = a + '' + b;
8+
var ba = b + '' + a;
9+
if (ab < ba) return 1;
10+
else if (ab > ba) return -1;
11+
return 0;
12+
});
13+
if (nums[0] === 0) return '0';
14+
return nums.join('');
15+
};
16+
17+
// this is a wrong solution e.g. [121,12] '12' < '121', then it doesn't work.
18+
var largestNumber = function(nums) {
19+
nums.sort(function(a, b) {
20+
if (a.toString() < b.toString()) return 1;
21+
else if (a.toString() > b.toString()) return -1;
22+
else return 0;
23+
});
24+
if (nums[0] === 0) return '0';
25+
return nums.join('');
26+
};

Medium/230-kthSmallestElementinBST.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,21 @@ var kthSmallest = function(root, k) {
2929
}
3030
}
3131
};
32+
33+
// second try
34+
var kthSmallest = function(root, k) {
35+
var stack = [];
36+
37+
while (root || stack.length > 0) {
38+
while (root) {
39+
stack.push(root);
40+
root = root.left;
41+
}
42+
root = stack.pop();
43+
k--;
44+
if (k === 0) return root.val;
45+
root = root.right;
46+
}
47+
48+
return 0;
49+
};

Medium/60-permutationSequence.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @see inline comments
3+
*
4+
* @param {number} n
5+
* @param {number} k
6+
* @return {string}
7+
*/
8+
var getPermutation = function(n, k) {
9+
var nums = [];
10+
for (var i = 0; i < n; i ++) {
11+
nums.push(i + 1);
12+
}
13+
k--;
14+
var mod = 1;
15+
for (i = 1; i < n; i++) {
16+
mod *= i;
17+
}
18+
var result = '';
19+
for (i = 0; i < n; i++) {
20+
// find the index of current number's first digit
21+
var index = Math.floor(k / mod);
22+
k = k % mod;
23+
result += nums[index];
24+
// remove this used number from nums.
25+
nums.splice(index, 1);
26+
mod = Math.floor(mod / (n - i - 1));
27+
}
28+
29+
return result;
30+
};

0 commit comments

Comments
 (0)