Skip to content

Commit ed0990f

Browse files
committed
add more solution
1 parent bbf5af3 commit ed0990f

13 files changed

+605
-85
lines changed

101 Symmetric Tree.js

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
2+
3+
// For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
4+
5+
// 1
6+
// / \
7+
// 2 2
8+
// / \ / \
9+
// 3 4 4 3
10+
// But the following [1,2,2,null,3,null,3] is not:
11+
// 1
12+
// / \
13+
// 2 2
14+
// \ \
15+
// 3 3
16+
// Note:
17+
// Bonus points if you could solve it both recursively and iteratively.
18+
19+
// Hide Company Tags LinkedIn Bloomberg Microsoft
20+
// Hide Tags Tree Depth-first Search Breadth-first Search
21+
22+
23+
124
/**
225
* Definition for a binary tree node.
326
* function TreeNode(val) {
@@ -10,55 +33,42 @@
1033
* @return {boolean}
1134
*/
1235
var isSymmetric = function(root) {
13-
if(root === null){
14-
return true;
15-
}
16-
1736
var queue = [];
1837
queue.push(root);
1938

20-
var temp = [];
21-
var curCnt = 1;
22-
var nextCnt = 0;
23-
24-
while(queue.length !== 0){
25-
var p = queue.shift();
39+
while(queue.length !== 0) {
40+
var len = queue.length;
2641

27-
if(p !== null){
28-
temp.push(p.left);
29-
temp.push(p.right);
42+
if(!isLevelSymmetric(queue)) {
43+
return false;
3044
}
3145

32-
if(queue.length === 0){
33-
if(isPalindrome(temp)){
34-
queue = temp;
35-
temp = [];
36-
} else {
37-
return false;
46+
for(var i = 0; i < len; i++) {
47+
var node = queue.shift();
48+
49+
if(node !== null) {
50+
queue.push(node.left);
51+
queue.push(node.right);
3852
}
3953
}
4054
}
4155

4256
return true;
4357
};
4458

45-
46-
var isPalindrome = function(arr){
47-
var head = 0;
48-
var tail = arr.length - 1;
59+
function isLevelSymmetric(nodes) {
60+
var len = nodes.length;
61+
var beg = 0;
62+
var end = len - 1;
4963

50-
while(head < tail){
51-
if(arr[head] && arr[tail]){
52-
if(arr[head].val !== arr[tail].val){
53-
return false;
54-
}
55-
} else if(arr[head] || arr[tail]){
64+
while(beg < end) {
65+
if(nodes[beg] === null && nodes[end] === null || (nodes[beg] && nodes[end] && nodes[beg].val === nodes[end].val)) {
66+
beg++;
67+
end--;
68+
} else {
5669
return false;
5770
}
58-
59-
head++;
60-
tail--;
6171
}
6272

6373
return true;
64-
}
74+
}
Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
// Leetcode #150
2-
// Language: Javascript
3-
// Problem: https://leetcode.com/problems/evaluate-reverse-polish-notation/
4-
// Author: Chihung Yu
1+
// Evaluate the value of an arithmetic expression in Reverse Polish Notation.
2+
3+
// Valid operators are +, -, *, /. Each operand may be an integer or another expression.
4+
5+
// Some examples:
6+
// ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
7+
// ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
8+
// Hide Company Tags LinkedIn
9+
// Hide Tags Stack
10+
// Hide Similar Problems (H) Basic Calculator (H) Expression Add Operators
11+
512
/**
613
* @param {string[]} tokens
714
* @return {number}
815
*/
916
var evalRPN = function(tokens) {
1017
var stack = [];
1118

12-
for(var i = 0; i < tokens.length; i++){
13-
var t = tokens[i];
14-
15-
if(t === '+'){
16-
var x = parseInt(stack.pop());
17-
var y = parseInt(stack.pop());
18-
stack.push(x + y);
19-
} else if(t === '-'){
20-
x = parseInt(stack.pop());
21-
y = parseInt(stack.pop());
22-
stack.push(y - x);
23-
} else if(t === '*'){
24-
x = parseInt(stack.pop());
25-
y = parseInt(stack.pop());
26-
stack.push(parseInt(y * x));
27-
} else if(t === '/'){
28-
x = parseInt(stack.pop());
29-
y = parseInt(stack.pop());
30-
stack.push(parseInt(y / x));
19+
for(var i = 0; i < tokens.length; i++) {
20+
var token = tokens[i];
21+
var val1,val2;
22+
var val = parseInt(token);
23+
if(!isNaN(val)) {
24+
stack.push(val);
3125
} else {
32-
stack.push(t);
26+
val2 = stack.pop();
27+
val1 = stack.pop();
28+
29+
if(token === '*') {
30+
stack.push(parseInt(val1 * val2));
31+
} else if(token === '/') {
32+
stack.push(parseInt(val1 / val2));
33+
} else if(token === '-') {
34+
stack.push(val1 - val2);
35+
} else if(token === '+') {
36+
stack.push(val1 + val2);
37+
}
3338
}
3439
}
35-
var num = stack.pop();
36-
return parseInt(num);
40+
41+
return stack.pop();
3742
};

152 Maximum Product Subarray.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1+
// Find the contiguous subarray within an array (containing at least one number) which has the largest product.
2+
3+
// For example, given the array [2,3,-2,4],
4+
// the contiguous subarray [2,3] has the largest product = 6.
5+
6+
// Hide Company Tags LinkedIn
7+
// Hide Tags Array Dynamic Programming
8+
// Show Similar Problems
9+
10+
11+
112
// Leetcode #152
213
// Language: Javascript
314
// Problem: https://leetcode.com/problems/maximum-product-subarray/
4-
// Author: Chihung Yu
515
/**
616
* @param {number[]} nums
717
* @return {number}
818
*/
9-
// http://www.programcreek.com/2014/03/leetcode-maximum-product-subarray-java/
19+
// reference: http://www.programcreek.com/2014/03/leetcode-maximum-product-subarray-java/
1020
var maxProduct = function(nums) {
1121
if(nums === null || nums.length === 0){
1222
return 0;

156 Binary Tree Upside Down.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.
2+
3+
// For example:
4+
// Given a binary tree {1,2,3,4,5},
5+
// 1
6+
// / \
7+
// 2 3
8+
// / \
9+
// 4 5
10+
// return the root of the binary tree [4,5,2,#,#,3,1].
11+
// 4
12+
// / \
13+
// 5 2
14+
// / \
15+
// 3 1
16+
// confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
17+
18+
// Hide Company Tags LinkedIn
19+
// Hide Tags Tree
20+
// Show Similar Problems
21+
22+
23+
24+
/**
25+
* Definition for a binary tree node.
26+
* function TreeNode(val) {
27+
* this.val = val;
28+
* this.left = this.right = null;
29+
* }
30+
*/
31+
/**
32+
* @param {TreeNode} root
33+
* @return {TreeNode}
34+
*/
35+
var upsideDownBinaryTree = function(root) {
36+
var newRoot = root;
37+
38+
generateUpsideDownHelper(root);
39+
40+
function generateUpsideDownHelper(root) {
41+
if(!root) {
42+
return root;
43+
}
44+
45+
if(!root.left && !root.right) {
46+
newRoot = root;
47+
return root;
48+
}
49+
50+
if(root.left) {
51+
var ret = generateUpsideDownHelper(root.left);
52+
ret.left = root.right;
53+
ret.right = root;
54+
root.left = null;
55+
root.right = null;
56+
}
57+
58+
return root;
59+
}
60+
return newRoot;
61+
};
62+
63+
64+
// simpler solution
65+
var upsideDownBinaryTree = function(root) {
66+
// second condition ensure the left most child will be the new root
67+
if (!root || (!root.left && !root.right)) {
68+
return root;
69+
}
70+
71+
let newRoot = upsideDownBinaryTree(root.left);
72+
console.log(newRoot.val, root.left)
73+
74+
root.left.left = root.right;
75+
root.left.right = root;
76+
77+
// cannot work if we sub root.left with newRoot
78+
// since new root is always the left most child
79+
// [doesn't work] newRoot.left = root.right;
80+
// [doesn't work] newRoot.right = root;
81+
82+
root.left = null;
83+
root.right = null;
84+
85+
return newRoot;
86+
};

187 Repeated DNA Sequences.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
2+
3+
// Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
4+
5+
// For example,
6+
7+
// Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",
8+
9+
// Return:
10+
// ["AAAAACCCCC", "CCCCCAAAAA"].
11+
// Hide Company Tags LinkedIn
12+
// Hide Tags Hash Table Bit Manipulation
13+
14+
15+
/**
16+
* @param {string} s
17+
* @return {string[]}
18+
*/
19+
var findRepeatedDnaSequences = function(s) {
20+
var hash = {};
21+
var result = [];
22+
23+
for(var i = 10; i <= s.length; i++) {
24+
var substr = s.substring(i - 10, i);
25+
if(hash[substr] === undefined) {
26+
hash[substr] = 1;
27+
} else if(hash[substr] === 1) {
28+
hash[substr]++;
29+
result.push(substr);
30+
}
31+
}
32+
33+
return result;
34+
};

236 Lowest Common Ancestor of a Binary Tree.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
* @param {TreeNode} q
3131
* @return {TreeNode}
3232
*/
33+
34+
// Amazon LinkedIn Apple Facebook Microsoft
35+
// Hide Tags Tree
36+
// Hide Similar Problems
3337

34-
// http://www.cnblogs.com/anne-vista/p/4815076.html
38+
// reference: http://www.cnblogs.com/anne-vista/p/4815076.html
3539
var lowestCommonAncestor = function(root, p, q) {
3640
if(root === null || root === p || root === q) {
3741
return root;

0 commit comments

Comments
 (0)