Skip to content

Commit 6e39eef

Browse files
committed
update 4 leetcode
1 parent aaec803 commit 6e39eef

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @lc app=leetcode id=637 lang=javascript
3+
*
4+
* [637] Average of Levels in Binary Tree
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* @param {TreeNode} root
18+
* @return {number[]}
19+
*/
20+
/**
21+
* Definition for a binary tree node.
22+
* function TreeNode(val, left, right) {
23+
* this.val = (val===undefined ? 0 : val)
24+
* this.left = (left===undefined ? null : left)
25+
* this.right = (right===undefined ? null : right)
26+
* }
27+
*/
28+
/**
29+
* @param {TreeNode} root
30+
* @return {number[]}
31+
*/
32+
var averageOfLevels = function(root) {
33+
let queues = [root];
34+
let level = 0;
35+
let res = [];
36+
37+
while (queues.length) {
38+
let oLen = len = queues.length;
39+
let sum = 0;
40+
while (len--) {
41+
const cur = queues.shift() || {};
42+
if (cur.val !== null) {
43+
sum += cur.val;
44+
}
45+
cur.left && queues.push(cur.left);
46+
cur.right && queues.push(cur.right);
47+
}
48+
res[level] = sum / oLen;
49+
level++;
50+
}
51+
52+
return res;
53+
};
54+
// @lc code=end
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @lc app=leetcode id=783 lang=javascript
3+
*
4+
* [783] Minimum Distance Between BST Nodes
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
17+
/**
18+
* 中序遍历的方式
19+
* 二叉搜索树的特性是任意两节点的差值肯定会比相邻节点的大
20+
* 下面就只遍历了一次
21+
* @param {*} root
22+
* @returns
23+
*/
24+
var minDiffInBST = function(root) {
25+
let ans = Number.MAX_SAFE_INTEGER, pre = -1;
26+
const dfs = (root) => {
27+
if (root === null) {
28+
return;
29+
}
30+
dfs(root.left);
31+
if (pre == -1) {
32+
pre = root.val;
33+
} else {
34+
ans = Math.min(ans, root.val - pre);
35+
pre = root.val;
36+
}
37+
dfs(root.right);
38+
}
39+
dfs(root);
40+
return ans;
41+
};
42+
43+
/**
44+
* 遍历保存所有值,进行排序
45+
* 再遍历保存差值
46+
* 复杂度较高
47+
* @param {TreeNode} root
48+
* @return {number}
49+
*/
50+
var minDiffInBST2 = function(root) {
51+
let queues = [root];
52+
let res = [];
53+
let min = Number.MAX_SAFE_INTEGER;
54+
55+
while (queues.length) {
56+
const cur = queues.shift() || {};
57+
if (cur.val !== null) {
58+
res.push(cur.val);
59+
}
60+
cur.left && queues.push(cur.left);
61+
cur.right && queues.push(cur.right);
62+
}
63+
64+
res = res.sort((a, b) => a - b);
65+
res.forEach((n, index) => {
66+
if (index == 0) {
67+
return;
68+
}
69+
if (Math.abs(n - res[index - 1]) < min) {
70+
min = Math.abs(n - res[index - 1]);
71+
}
72+
});
73+
74+
return min;
75+
};
76+
// @lc code=end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @lc app=leetcode id=876 lang=javascript
3+
*
4+
* [876] Middle of the Linked List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* 快慢指针法
17+
* @param {ListNode} head
18+
* @return {ListNode}
19+
*/
20+
var middleNode = function(head) {
21+
let fast = slow = head;
22+
23+
// 慢的走一步,快的走两步,这样快的走到的时候,慢的正好走到一半
24+
while (fast && fast.next) {
25+
slow = slow.next;
26+
fast = fast.next.next;
27+
}
28+
29+
return slow;
30+
};
31+
32+
/**
33+
* 单指针法
34+
* @param {*} head
35+
* @returns
36+
*/
37+
var middleNode2 = function(head) {
38+
if (!head.next) {
39+
return head;
40+
}
41+
let h1 = h2 = head;
42+
let l1 = l2 = 0;
43+
44+
while (h1.next) {
45+
h1 = h1.next;
46+
l1++;
47+
}
48+
const mid = ~~((l1 + 1) / 2);
49+
// 这里也不一定使用循环,可以用数组保存数据,然后直接通过下标访问
50+
while (l2 < mid) {
51+
h2 = h2.next;
52+
l2++;
53+
}
54+
55+
return h2;
56+
};
57+
// @lc code=end

leetcode/938.range-sum-of-bst.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=938 lang=javascript
3+
*
4+
* [938] Range Sum of BST
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* 广度优先的遍历
18+
* @param {TreeNode} root
19+
* @param {number} low
20+
* @param {number} high
21+
* @return {number}
22+
*/
23+
var rangeSumBST = function(root, low, high) {
24+
let queues = [root];
25+
let sum = 0;
26+
27+
while (queues.length) {
28+
const cur = queues.shift() || {};
29+
if (cur.val <= high && cur.val >= low) {
30+
sum += cur.val;
31+
}
32+
cur.left && queues.push(cur.left);
33+
cur.right && queues.push(cur.right);
34+
}
35+
return sum;
36+
};
37+
// @lc code=end

0 commit comments

Comments
 (0)