Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d07b32b

Browse files
feat: add 10 leetcode about binary tree and so on
1 parent 35b49d0 commit d07b32b

10 files changed

+422
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @lc app=leetcode id=129 lang=typescript
3+
*
4+
* [129] Sum Root to Leaf Numbers
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* class TreeNode {
11+
* val: number
12+
* left: TreeNode | null
13+
* right: TreeNode | null
14+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.left = (left===undefined ? null : left)
17+
* this.right = (right===undefined ? null : right)
18+
* }
19+
* }
20+
*/
21+
22+
/**
23+
* @description: 深度优先的遍历
24+
* 时间复杂度 O(n)
25+
* 空间复杂度 O(n)
26+
* @param {TreeNode} root
27+
* @return {number}
28+
*/
29+
function sumNumbers(root: TreeNode | null): number {
30+
let sum = 0; // 保存最终的和
31+
32+
/**
33+
* @description: 深度优先的搜索
34+
* @param {TreeNode} root
35+
* @param {string} val
36+
* @return {void}
37+
*/
38+
function dfs(node: TreeNode, val: string): void {
39+
val += node.val.toString();
40+
// 如果没有子节点,说明已经到达当前叶子节点
41+
if (!(node.left || node.right)) {
42+
sum += +val;
43+
return;
44+
}
45+
node.left && dfs(node.left, val);
46+
node.right && dfs(node.right, val);
47+
}
48+
49+
dfs(root, '');
50+
51+
return sum;
52+
}
53+
// @lc code=end

‎leetcode/141.linked-list-cycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ function hasCycle(head: MyListNode | null): boolean {
4848

4949
// 如果完成遍历,则说明没有环
5050
return false;
51-
};
51+
}
5252
// @lc code=end

‎leetcode/20.valid-parentheses.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=20 lang=typescript
3+
*
4+
* [20] Valid Parentheses
5+
*/
6+
7+
// @lc code=start
8+
9+
type left = '{' | '[' | '(';
10+
/**
11+
* @description: 栈的思路
12+
* @param {string} s
13+
* @return {boolean}
14+
*/
15+
function isValid(s: string): boolean {
16+
// 定义对象方便存储
17+
const parentMap = {
18+
'}': '{',
19+
']': '[',
20+
')': '(',
21+
};
22+
const stack: left[] = [];
23+
24+
for (const str of s) {
25+
// 如果遇到开括号就推入栈
26+
if (str === '{' || str === '[' || str === '(') {
27+
stack.push(str as left);
28+
continue;
29+
}
30+
// 如果遇到闭括号就判断栈尾是不是对应的括号,不对应时可直接返回结果,对应则弹出栈尾的括号
31+
if (stack[stack.length - 1] !== parentMap[str]) return false;
32+
stack.pop();
33+
}
34+
35+
return !stack.length;
36+
}
37+
// @lc code=end

‎leetcode/278.first-bad-version.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* };
1313
*/
1414

15-
var solution = function (isBadVersion: any) {
15+
var solution1 = function (isBadVersion: any) {
1616
return function (n: number): number {
1717
let min = 1;
1818
let max = n;
@@ -52,4 +52,23 @@ var solution2 = function (isBadVersion: any) {
5252
}
5353
};
5454
};
55+
56+
// 优化后的写法
57+
var solution = function (isBadVersion: any) {
58+
return function (n: number): number {
59+
let min = 1;
60+
let max = n;
61+
62+
while (min <= max) {
63+
const mid = Math.floor(min + ((max - min) >> 1));
64+
65+
if (isBadVersion(mid)) {
66+
if (!isBadVersion(mid - 1)) return mid;
67+
max = mid - 1;
68+
} else {
69+
min = mid + 1;
70+
}
71+
}
72+
};
73+
};
5574
// @lc code=end

‎leetcode/507.perfect-number.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @lc app=leetcode id=507 lang=typescript
3+
*
4+
* [507] Perfect Number
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @description: 更新除数的方式(枚举)
10+
* 时间复杂度 O(√num)
11+
* 空间复杂度 O(1)
12+
* @param {number} num
13+
* @return {boolean}
14+
*/
15+
function checkPerfectNumber2(num: number): boolean {
16+
if (num === 1) return false;
17+
18+
let sum = 1;
19+
20+
for (let i = 2; i * i <= num; i++) {
21+
// 如果能除尽,继续叠加;以及更新除数
22+
if (num % i === 0) {
23+
sum += i;
24+
if (i * i < num) {
25+
sum += Math.floor(num / i);
26+
}
27+
}
28+
}
29+
30+
return sum === num;
31+
}
32+
33+
/**
34+
* @description: 数学
35+
* 时间和空间复杂度都是 O(1)
36+
* @param {number} num
37+
* @return {boolean}
38+
*/
39+
function checkPerfectNumber(num: number): boolean {
40+
return num === 6 || num === 28 || num === 496 || num === 8128 || num === 33550336;
41+
}
42+
// @lc code=end

‎leetcode/561.array-partition-i.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @description: 计数排序,再直接累加
3+
* 时间复杂度 O(n) 两次遍历
4+
* 空间复杂度 O(n)
5+
* @param {number} nums
6+
* @return {number}
7+
*/
8+
function arrayPairSum(nums: number[]): number {
9+
const arr = new Array(20000);
10+
let ans = 0;
11+
12+
// 计数排序,统计数量
13+
for (let i = 0; i < nums.length; i++) {
14+
const element = nums[i] + 10000;
15+
arr[element] = (arr[element] || 0) + 1;
16+
}
17+
18+
let isOdd = true;
19+
// 遍历过程中如果是奇数列就直接累加
20+
for (let i = 0; i < arr.length; i++) {
21+
if (arr[i] === undefined) continue;
22+
const num = i - 10000;
23+
24+
while (arr[i]--) {
25+
if (isOdd) {
26+
ans += num;
27+
}
28+
isOdd = !isOdd;
29+
}
30+
}
31+
32+
return ans;
33+
}

‎leetcode/563.binary-tree-tilt.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @lc app=leetcode id=563 lang=typescript
3+
*
4+
* [563] Binary Tree Tilt
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* class TreeNode {
11+
* val: number
12+
* left: TreeNode | null
13+
* right: TreeNode | null
14+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.left = (left===undefined ? null : left)
17+
* this.right = (right===undefined ? null : right)
18+
* }
19+
* }
20+
*/
21+
22+
/**
23+
* @description: 递归计算结果
24+
* @param {TreeNode} root
25+
* @return {number}
26+
*/
27+
function findTilt(root: TreeNode | null): number {
28+
if (root === null) return 0;
29+
30+
let ans = 0;
31+
32+
/**
33+
* @description: 递归遍历计算
34+
* @param {TreeNode} node
35+
* @return {number}
36+
*/
37+
function dfs(node: TreeNode): number {
38+
if (node === null) return 0;
39+
40+
const left = dfs(node.left);
41+
const right = dfs(node.right);
42+
43+
ans += Math.abs(left - right);
44+
45+
return left + right + node.val;
46+
}
47+
48+
dfs(root);
49+
50+
return ans;
51+
}
52+
// @lc code=end

‎leetcode/655.print-binary-tree.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @lc app=leetcode id=655 lang=typescript
3+
*
4+
* [655] Print Binary Tree
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* class TreeNode {
11+
* val: number
12+
* left: TreeNode | null
13+
* right: TreeNode | null
14+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.left = (left===undefined ? null : left)
17+
* this.right = (right===undefined ? null : right)
18+
* }
19+
* }
20+
*/
21+
22+
/**
23+
* @description: 递归填充值的思路
24+
* 时间复杂度 O(n)
25+
* 空间复杂度 O(n)
26+
* @param {TreeNode} root
27+
* @return {string[][]}
28+
*/
29+
function printTree(root: TreeNode | null): string[][] {
30+
/**
31+
* @description: 递归获取最大深度
32+
* @param {TreeNode} root
33+
* @return {number}
34+
*/
35+
function getDepth(root: TreeNode): number {
36+
if (root === null) return 0;
37+
// !从左子树和右子树中取出最大的深度;
38+
return 1 + Math.max(getDepth(root.left), getDepth(root.right));
39+
}
40+
41+
const depth = getDepth(root);
42+
const width = Math.pow(2, depth) - 1;
43+
// 根据高度和宽度初始化数组
44+
const ans = new Array(depth).fill(null).map(() => new Array(width).fill(''));
45+
46+
/**
47+
* @description: 递归遍历并填充值
48+
* 每层遍历都更新当前行和当前列
49+
* @param {TreeNode} node
50+
* @param {number} row
51+
* @param {number} col
52+
* @return {void}
53+
*/
54+
function fillByDfs(node: TreeNode, row: number, col: number): void {
55+
if (node === null) return;
56+
ans[row][col] = node.val.toString();
57+
58+
const nextRow = row + 1;
59+
// 实际高度,因为索引从 0 开始计算
60+
const height = depth - 1;
61+
// !计算当前层的列数的差值,每层的列的值是从中间向两边散开的
62+
const diffCol = Math.pow(2, height - nextRow);
63+
fillByDfs(node.left, nextRow, col - diffCol);
64+
fillByDfs(node.right, nextRow, col + diffCol);
65+
}
66+
67+
// 初始化的值,首行的列是在数组中间
68+
fillByDfs(root, 0, width >> 1);
69+
70+
return ans;
71+
}
72+
// @lc code=end

‎leetcode/912.sort-an-array.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
// @lc code=start
88
/**
99
* @description: 计数排序的实现方案
10+
* 时间复杂度 O(n)
11+
* 空间复杂度 O(n)
12+
* 或者可以说是 O(1) 已知的常量是 100000
1013
* @param {number[]} nums
1114
* @return {number[]}
1215
*/
1316
function sortArray(nums: number[]): number[] {
1417
// 已知是整数且大小固定;有负数就通过补位来保证索引是正的
1518
let cnt: number[] = new Array(100001);
16-
let res = [];
19+
let res: number[] = [];
1720

1821
// 遍历一遍原数组,保存每个值的出现次数在另一个数组的索引位置
1922
for (let i = 0; i < nums.length; i++) {
@@ -30,5 +33,5 @@ function sortArray(nums: number[]): number[] {
3033
}
3134
}
3235
return res;
33-
};
36+
}
3437
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /