Skip to content

Commit 51f69d2

Browse files
committed
feat: btree && link-list
1 parent 31e7386 commit 51f69d2

13 files changed

+283
-21
lines changed

BinaryTree/banlenced-b-tree.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
function getDepth(Tree) {
3+
if (!Tree) return 0
4+
const left = getDepth(Tree.left)
5+
const right = getDepth(Tree.right)
6+
// 左平衡 && 右平衡 && 左右高度 <= 1
7+
if (left === -1 || right === -1 || Math.abs(left - right) > 1) {
8+
return -1
9+
}
10+
if (left > right) {
11+
return left + 1
12+
}
13+
return right + 1
14+
}
15+
16+
function banlencedBTree(Tree) {
17+
return getDepth(Tree) !== -1
18+
}
19+
20+
const data = require('./generateBTree')
21+
22+
console.log(banlencedBTree(data))

BinaryTree/bfs-tree.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function bfsTree(Tree) {
2+
const res = []
3+
const stack = [Tree]
4+
while (stack.length) {
5+
const node = stack.shift()
6+
res.push(node.val)
7+
node.left && stack.push(node.left)
8+
node.right && stack.push(node.right)
9+
}
10+
return res
11+
}
12+
13+
const data = require('./generateBTree')
14+
15+
console.log(bfsTree(data))

BinaryTree/generateBTree.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
3+
4+
function genTree(Tree, depth) {
5+
if (depth === 0) {
6+
Tree = null;
7+
return;
8+
}
9+
if (!Tree.val) {
10+
Tree.val = Math.ceil(Math.random() * 10);
11+
Tree.left = {};
12+
Tree.right = {};
13+
genTree(Tree.left, depth - 1);
14+
genTree(Tree.right, depth - 1);
15+
}
16+
}
17+
18+
const tree = {}
19+
genTree(tree, 3)
20+
21+
module.exports = {
22+
"val": 1,
23+
"left": {
24+
"val": 2,
25+
"left": { "val": 4, "left": null, "right": null },
26+
"right": { "val": 5, "left": null, "right": null }
27+
},
28+
"right": {
29+
"val": 3,
30+
"left": { "val": 6, "left": null, "right": null },
31+
"right": { "val": 7, "left": null, "right": null }
32+
}
33+
}

BinaryTree/in-order-traversal.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
function inOrderTraversal1(Tree, res) {
4+
if (!Tree) return
5+
inOrderTraversal1(Tree.left, res)
6+
res.push(Tree.val)
7+
inOrderTraversal1(Tree.right, res)
8+
}
9+
10+
function inOrderTraversal2(Tree) {
11+
const res = []
12+
const stack = []
13+
while (Tree || stack.length) {
14+
while (Tree) {
15+
stack.push(Tree)
16+
Tree = Tree.left
17+
}
18+
const node = stack.pop()
19+
res.push(node.val)
20+
Tree = node.right
21+
}
22+
return res
23+
}
24+
25+
const data = require('./generateBTree')
26+
const r = []
27+
inOrderTraversal1(data, r)
28+
const res = inOrderTraversal2(data)
29+
30+
console.log(res)

BinaryTree/max-depth-sum.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Math.max(左边最大深度 + 父节点, 右边最大深度 + 父节点)
2+
function maxPathSum(Tree) {
3+
if (!Tree) return 0
4+
const left = Math.max(maxPathSum(Tree.left), 0)
5+
const right = Math.max(maxPathSum(Tree.right), 0)
6+
if (left > right) {
7+
return left + Tree.val
8+
}
9+
return right + Tree.val
10+
}
11+
12+
const data = require('./generateBTree')
13+
14+
console.log(maxPathSum(data))

BinaryTree/max-depth.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function maxDepth(Tree) {
2+
if (!Tree) return 0
3+
const left = maxDepth(Tree.left)
4+
const right = maxDepth(Tree.right)
5+
if (left > right) {
6+
return left + 1
7+
}
8+
return right + 1
9+
}

BinaryTree/post-order-traversal.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
3+
function postOrderTraversal1(Tree, res) {
4+
if (!Tree) return
5+
postOrderTraversal1(Tree.left, res)
6+
postOrderTraversal1(Tree.right, res)
7+
res.push(Tree.val)
8+
}
9+
10+
function postOrderTraversal2(Tree) {
11+
const res = []
12+
const stack = []
13+
let lastVisited = Tree
14+
while (Tree || stack.length) {
15+
while (Tree) {
16+
stack.push(Tree)
17+
Tree = Tree.left
18+
}
19+
const node = stack[stack.length - 1]
20+
// 重点:先遍历了右儿子才能遍历自身
21+
if (!node.right || node.right === lastVisited) {
22+
stack.pop()
23+
lastVisited = node
24+
res.push(node.val)
25+
} else {
26+
Tree = node.right
27+
}
28+
}
29+
return res
30+
}
31+
32+
const data = require('./generateBTree')
33+
const r = []
34+
postOrderTraversal1(data, r)
35+
const res = postOrderTraversal2(data)
36+
37+
console.log(res)

BinaryTree/pre-order-traversal.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
function preOrderTranversal1(Tree, res) {
4+
if (!Tree) return
5+
res.push(Tree.val)
6+
preOrderTranversal1(Tree.left, res)
7+
preOrderTranversal1(Tree.right, res)
8+
}
9+
10+
function preOrderTranversal2(Tree) {
11+
const res = []
12+
const stack = []
13+
while (Tree || stack.length) {
14+
while (Tree) {
15+
stack.push(Tree)
16+
res.push(Tree.val)
17+
Tree = Tree.left
18+
}
19+
const node = stack.pop()
20+
Tree = node.right
21+
}
22+
return res
23+
}
24+
25+
const data = require('./generateBTree')
26+
27+
const res = preOrderTranversal2(data)
28+
29+
console.log(res)

ByteDance/curry.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1-
function curry(fn) {
2-
var argArr = Array.prototype.slice.call(arguments, 1); // curry的第二个参数以及后面的参数,初始值通常情况下是[]
3-
var len = fn.length; // 待柯里化函数的参数个数
4-
return function () {
5-
// 获得本轮递归执行的函数的参数
6-
var innerArgs = Array.prototype.slice.call(arguments);
7-
// 将上一轮递归的参数与本轮递归的参数进行合并
8-
// 注意: argArr 里面缓存了上一轮递归执行参数合并后的结果
9-
argArr = argArr.concat(innerArgs);
10-
if (argArr.length === len) {
11-
// 如果最终合并后的参数个数,与被柯里化的函数的参数个数相同
12-
// 那么将合并后的参数作为被柯里化函数的参数,执行
13-
// 相当于提取所有的参数,然后拿到原函数里面去执行
14-
var result = fn.apply(null, argArr);
15-
// 到这里就执行完成了
16-
argArr = [];
17-
return result;
1+
function curry(fn, args) {
2+
args = args || []
3+
const length = fn.length
4+
return function() {
5+
const curArgs = Array.prototype.slice.call(arguments, 0)
6+
const newArgs = args.concat(curArgs)
7+
if (newArgs.length === length) {
8+
return fn.apply(this, newArgs)
189
} else {
19-
// 返回正在执行的函数,也就是函数自身,相当于递归
20-
// 但是递归层数是动态的
21-
// 这里会把本轮递归的参数通过 argArr 带到下一轮递归中
22-
return arguments.callee;
10+
return curry(fn, newArgs)
2311
}
2412
}
2513
}

linked-list/delete-duplicates.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
function deleteDuplicates(head) {
4+
let cur = head
5+
while (cur) {
6+
while (cur.next && cur.val === cur.next.val) {
7+
cur.next = cur.next.next
8+
}
9+
cur = cur.next
10+
}
11+
return head
12+
}
13+
14+
const { generateLinkList, outputLinkList } = require('./generateLinkList')
15+
16+
console.log(outputLinkList(deleteDuplicates(generateLinkList([1, 1, 2, 3, 3, 3, 4, 4, 5]))))

linked-list/generateLinkList.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function generateLinkList(arr = []) {
2+
let head = {}
3+
const res = head
4+
for (let index = 0; index < arr.length; index++) {
5+
const cur = arr[index]
6+
head.val = cur
7+
head.next = index === arr.length - 1 ? null : {}
8+
head = head.next
9+
}
10+
return res
11+
}
12+
13+
function outputLinkList(head) {
14+
const res = []
15+
while (head) {
16+
res.push(head.val)
17+
head = head.next
18+
}
19+
return res
20+
}
21+
22+
module.exports = {
23+
outputLinkList,
24+
generateLinkList
25+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function removeElements(head, val) {
14+
let cur = { val: null, next: head }
15+
let result = cur
16+
let pre = cur
17+
cur = cur.next
18+
while(cur) {
19+
if (cur.val === val) {
20+
pre.next = cur.next
21+
cur = pre.next
22+
} else {
23+
pre = pre.next
24+
cur = cur.next
25+
}
26+
}
27+
return result.next
28+
};

linked-list/reverse-list.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
function reverseList(head) {
3+
let cur = head
4+
let res = null
5+
while (cur) {
6+
const next = cur.next
7+
cur.next = res
8+
res = cur
9+
cur = next
10+
}
11+
return res
12+
}
13+
14+
const { generateLinkList, outputLinkList } = require('./generateLinkList')
15+
16+
console.log(outputLinkList(reverseList(generateLinkList([1,2,3,4,5]))))

0 commit comments

Comments
 (0)