Skip to content

Commit 7c0ba13

Browse files
committed
format
1 parent 2944f5f commit 7c0ba13

15 files changed

+387
-366
lines changed

000__easy__temp.js

-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@
1010
*
1111
* 10m
1212
*/
13-
14-

101__easy__symmetric-tree.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@
3030
* 15m
3131
*/
3232
var isSymmetric = function(root) {
33-
if (!root) return true;
34-
const isMirr = (a, b) => {
35-
if (!a && !b) return true;
36-
if (!a || !b || a.val !== b.val) return false;
37-
return isMirr(a.left, b.right) && isMirr(a.right, b.left);
38-
}
39-
return isMirr(root.left, root.right);
33+
if (!root) return true;
34+
const isMirr = (a, b) => {
35+
if (!a && !b) return true;
36+
if (!a || !b || a.val !== b.val) return false;
37+
return isMirr(a.left, b.right) && isMirr(a.right, b.left);
38+
};
39+
return isMirr(root.left, root.right);
4040
};
4141

42-
4342
/**
4443
* Iterative
4544
*
@@ -49,20 +48,20 @@ var isSymmetric = function(root) {
4948
* 15m
5049
*/
5150
var isSymmetric = function(root) {
52-
if (!root) return true;
51+
if (!root) return true;
5352

54-
let queue = [root.left, root.right];
55-
while (queue.length > 0) {
56-
let t1 = queue.shift();
57-
let t2 = queue.shift();
53+
let queue = [root.left, root.right];
54+
while (queue.length > 0) {
55+
let t1 = queue.shift();
56+
let t2 = queue.shift();
5857

59-
if (!t1 && !t2) continue;
60-
if (!t1 || !t2 || t1.val !== t2.val) return false;
58+
if (!t1 && !t2) continue;
59+
if (!t1 || !t2 || t1.val !== t2.val) return false;
6160

62-
queue.push(t1.left);
63-
queue.push(t2.right);
64-
queue.push(t1.right);
65-
queue.push(t2.left);
66-
}
67-
return true;
68-
};
61+
queue.push(t1.left);
62+
queue.push(t2.right);
63+
queue.push(t1.right);
64+
queue.push(t2.left);
65+
}
66+
return true;
67+
};

102__medium__binary-tree-level-order-traversal.js

+26-28
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
2121
*/
2222

23-
2423
/**
2524
* Recursive
2625
* O(n) time
@@ -29,22 +28,21 @@
2928
* 25m
3029
*/
3130
var levelOrder = function(root) {
32-
let res = [];
33-
if (!root) return res;
34-
const preorder = (node, level=0) => {
35-
if (!node) return;
31+
let res = [];
32+
if (!root) return res;
33+
const preorder = (node, level = 0) => {
34+
if (!node) return;
3635

37-
if (level > res.length - 1) res.push([]);
36+
if (level > res.length - 1) res.push([]);
3837

39-
res[level].push(node.val);
40-
preorder(node.left, level+1);
41-
preorder(node.right, level+1);
42-
}
43-
preorder(root);
44-
return res;
38+
res[level].push(node.val);
39+
preorder(node.left, level + 1);
40+
preorder(node.right, level + 1);
41+
};
42+
preorder(root);
43+
return res;
4544
};
4645

47-
4846
/**
4947
* Iteratively using queue
5048
* O(n) time
@@ -53,23 +51,23 @@ var levelOrder = function(root) {
5351
* 10m
5452
*/
5553
var levelOrder = function(root) {
56-
let res = [];
57-
let queue = [];
54+
let res = [];
55+
let queue = [];
5856

59-
if (!root) return res;
60-
queue.push(root);
57+
if (!root) return res;
58+
queue.push(root);
6159

62-
while (queue.length > 0) {
63-
let lvlen = queue.length;
64-
let curr = [];
65-
for (let i = 0; i < lvlen; i++) {
66-
let node = queue.shift();
67-
curr.push(node.val);
68-
if (node.left) queue.push(node.left);
69-
if (node.right) queue.push(node.right);
70-
}
71-
res.push(curr);
60+
while (queue.length > 0) {
61+
let lvlen = queue.length;
62+
let curr = [];
63+
for (let i = 0; i < lvlen; i++) {
64+
let node = queue.shift();
65+
curr.push(node.val);
66+
if (node.left) queue.push(node.left);
67+
if (node.right) queue.push(node.right);
7268
}
69+
res.push(curr);
70+
}
7371

74-
return res;
72+
return res;
7573
};

104__easy__maximum-depth-of-binary-tree.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@
2727
* 20m
2828
*/
2929
var maxDepth = function(root) {
30-
if (!root) return 0;
30+
if (!root) return 0;
3131

32-
let max = 1;
33-
topDown(root, max, 0);
34-
return max;
32+
let max = 1;
33+
topDown(root, max, 0);
34+
return max;
3535

36-
function topDown(node, depth) {
37-
if (!node) return;
36+
function topDown(node, depth) {
37+
if (!node) return;
3838

39-
if (!node.left && !node.right) {
40-
max = Math.max(max, depth);
41-
}
42-
43-
topDown(node.left, depth + 1);
44-
topDown(node.right, depth + 1);
39+
if (!node.left && !node.right) {
40+
max = Math.max(max, depth);
4541
}
42+
43+
topDown(node.left, depth + 1);
44+
topDown(node.right, depth + 1);
45+
}
4646
};
4747

4848
/**
@@ -53,12 +53,12 @@ var maxDepth = function(root) {
5353
* 10m
5454
*/
5555
var maxDepth = function(root) {
56-
if (!root) return 0;
56+
if (!root) return 0;
5757

58-
let maxLeft = maxDepth(root.left);
59-
let maxRight = maxDepth(root.right);
58+
let maxLeft = maxDepth(root.left);
59+
let maxRight = maxDepth(root.right);
6060

61-
return 1 + Math.max(maxLeft, maxRight);
61+
return 1 + Math.max(maxLeft, maxRight);
6262
};
6363

6464
/**
@@ -69,19 +69,19 @@ var maxDepth = function(root) {
6969
* 15m
7070
*/
7171
var maxDepth = function(root) {
72-
let res = 0;
73-
let queue = [];
74-
if (!root) return res;
75-
76-
queue.push(root);
77-
while (queue.length > 0) {
78-
const q = queue.length;
79-
for (let i = 0; i < q; i++) {
80-
let node = queue.shift();
81-
if (node.left) queue.push(node.left);
82-
if (node.right) queue.push(node.right);
83-
}
84-
res++;
72+
let res = 0;
73+
let queue = [];
74+
if (!root) return res;
75+
76+
queue.push(root);
77+
while (queue.length > 0) {
78+
const q = queue.length;
79+
for (let i = 0; i < q; i++) {
80+
let node = queue.shift();
81+
if (node.left) queue.push(node.left);
82+
if (node.right) queue.push(node.right);
8583
}
86-
return res;
84+
res++;
85+
}
86+
return res;
8787
};

105__medium__construct-binary-tree-from-preorder-and-inorder-traversal.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ Return the following binary tree:
2727
* 30m ... ...
2828
*/
2929
var buildTree = function(preorder, inorder) {
30-
let p = 0;
30+
let p = 0;
3131

32-
const helper = (l, r) => {
33-
if (l > r) return null;
32+
const helper = (l, r) => {
33+
if (l > r) return null;
3434

35-
let root_val = preorder[p++];
36-
let root = new TreeNode(root_val);
35+
let root_val = preorder[p++];
36+
let root = new TreeNode(root_val);
3737

38-
let idx = inorder.indexOf(root_val);
38+
let idx = inorder.indexOf(root_val);
3939

40-
// recursion
41-
root.left = helper(l, idx - 1);
42-
root.right = helper(idx + 1, r);
43-
return root;
44-
};
40+
// recursion
41+
root.left = helper(l, idx - 1);
42+
root.right = helper(idx + 1, r);
43+
return root;
44+
};
4545

46-
return helper(0, inorder.length - 1);
46+
return helper(0, inorder.length - 1);
4747
};
4848

4949
/**
@@ -54,25 +54,25 @@ var buildTree = function(preorder, inorder) {
5454
* 30m ... ...
5555
*/
5656
var buildTree = function(preorder, inorder) {
57-
let p = 0;
58-
let mp = {};
59-
for (let i = 0; i < inorder.length; i++) {
60-
mp[inorder[i]] = i;
61-
}
57+
let p = 0;
58+
let mp = {};
59+
for (let i = 0; i < inorder.length; i++) {
60+
mp[inorder[i]] = i;
61+
}
6262

63-
const helper = (l, r) => {
64-
if (l > r) return null;
63+
const helper = (l, r) => {
64+
if (l > r) return null;
6565

66-
let root_val = preorder[p++];
67-
let root = new TreeNode(root_val);
66+
let root_val = preorder[p++];
67+
let root = new TreeNode(root_val);
6868

69-
let idx = mp[root_val];
69+
let idx = mp[root_val];
7070

71-
// recursion
72-
root.left = helper(l, idx - 1);
73-
root.right = helper(idx + 1, r);
74-
return root;
75-
};
71+
// recursion
72+
root.left = helper(l, idx - 1);
73+
root.right = helper(idx + 1, r);
74+
return root;
75+
};
7676

77-
return helper(0, inorder.length - 1);
77+
return helper(0, inorder.length - 1);
7878
};

106__medium__construct-binary-tree-from-inorder-and-postorder-traversal.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ Return the following binary tree:
3131
*
3232
*/
3333
var buildTree = function(inorder, postorder) {
34-
let p = postorder.length - 1;
35-
let map = {};
36-
for (let i = 0; i < inorder.length; i++) {
37-
map[inorder[i]] = i;
38-
}
34+
let p = postorder.length - 1;
35+
let map = {};
36+
for (let i = 0; i < inorder.length; i++) {
37+
map[inorder[i]] = i;
38+
}
3939

40-
const helper = (l, r) => {
41-
if (l > r) return null;
40+
const helper = (l, r) => {
41+
if (l > r) return null;
4242

43-
let root_val = postorder[p--];
44-
let root = new TreeNode(root_val);
43+
let root_val = postorder[p--];
44+
let root = new TreeNode(root_val);
4545

46-
let idx = map[root_val];
46+
let idx = map[root_val];
4747

48-
root.right = helper(idx + 1, r);
49-
root.left = helper(l, idx - 1);
50-
return root;
51-
};
48+
root.right = helper(idx + 1, r);
49+
root.left = helper(l, idx - 1);
50+
return root;
51+
};
5252

53-
return helper(0, p);
53+
return helper(0, p);
5454
};

112__easy__path-sum.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
2727
* 12m
2828
*/
2929
var hasPathSum = function(root, sum) {
30-
let found = false;
30+
let found = false;
3131

32-
const topDown = (node, curr = 0) => {
33-
if (found || !node) return;
34-
curr += node.val;
32+
const topDown = (node, curr = 0) => {
33+
if (found || !node) return;
34+
curr += node.val;
3535

36-
if (node.left == null && node.right == null) {
37-
found = curr == sum ? true : false;
38-
return;
39-
}
40-
if (node.left !== null) topDown(node.left, curr);
41-
if (node.right !== null) topDown(node.right, curr);
42-
};
36+
if (node.left == null && node.right == null) {
37+
found = curr == sum ? true : false;
38+
return;
39+
}
40+
if (node.left !== null) topDown(node.left, curr);
41+
if (node.right !== null) topDown(node.right, curr);
42+
};
4343

44-
topDown(root);
44+
topDown(root);
4545

46-
return found;
46+
return found;
4747
};
4848

4949
/*

0 commit comments

Comments
 (0)