Skip to content

Commit 36b4f6f

Browse files
committed
update 4 leetcode
1 parent dae2e4b commit 36b4f6f

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @lc app=leetcode id=103 lang=javascript
3+
*
4+
* [103] Binary Tree Zigzag Level Order Traversal
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+
* @return {number[][]}
20+
*/
21+
var zigzagLevelOrder = function(root) {
22+
let queues = [root];
23+
let level = 0;
24+
let ans = [];
25+
26+
while (queues.length) {
27+
let len = queues.length;
28+
// 用一个参数来标识变化
29+
let isOdd = level % 2 === 0;
30+
31+
while (len--) {
32+
let cur = queues.shift();
33+
if (!cur) {
34+
continue;
35+
}
36+
ans[level] = ans[level] || [];
37+
// 根据当前奇数偶数来判断推进数组的方向
38+
if (isOdd) {
39+
ans[level].push(cur.val);
40+
} else {
41+
ans[level].unshift(cur.val);
42+
}
43+
queues.push(cur.left);
44+
queues.push(cur.right);
45+
}
46+
level++;
47+
}
48+
49+
return ans;
50+
};
51+
// @lc code=end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode id=107 lang=javascript
3+
*
4+
* [107] Binary Tree Level Order Traversal II
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+
* @return {number[][]}
20+
*/
21+
var levelOrderBottom = function(root) {
22+
let queues = [root];
23+
let res = [];
24+
let level = 0;
25+
26+
while (queues.length) {
27+
let len = queues.length;
28+
29+
while (len--) {
30+
const cur = queues.shift();
31+
if (!cur) {
32+
continue;
33+
}
34+
res[level] = res[level] || [];
35+
res[level].push(cur.val);
36+
queues.push(cur.left);
37+
queues.push(cur.right);
38+
}
39+
level++;
40+
}
41+
42+
return res.reverse();
43+
};
44+
// @lc code=end

leetcode/257.binary-tree-paths.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @lc app=leetcode id=257 lang=javascript
3+
*
4+
* [257] Binary Tree Paths
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+
* @return {string[]}
20+
*/
21+
var binaryTreePaths = function(root) {
22+
let ans = []; // 用来保存结果
23+
// let str = '' 不可以外部保存这样
24+
25+
/**
26+
* 核心是把字符串作为参数传递,这样就可以保存每个节点的历史值而不至于被清空
27+
* @param {*} node
28+
* @param {*} str
29+
*/
30+
function dfsWithStr(node, str = '') {
31+
if (!node) {
32+
return null;
33+
}
34+
str += node.val;
35+
if (node.left === null && node.right === null) {
36+
ans.push(str);
37+
} else {
38+
str += '->';
39+
dfsWithStr(node.left, str);
40+
dfsWithStr(node.right, str);
41+
}
42+
}
43+
44+
dfsWithStr(root, '');
45+
46+
return ans;
47+
};
48+
// @lc code=end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode id=513 lang=javascript
3+
*
4+
* [513] Find Bottom Left Tree Value
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+
* @return {number}
20+
*/
21+
var findBottomLeftValue = function(root) {
22+
let queues = [root];
23+
let level = 0;
24+
let ans = [];
25+
26+
while (queues.length) {
27+
let len = queues.length;
28+
29+
while (len--) {
30+
let cur = queues.shift();
31+
if (!cur) {
32+
continue;
33+
}
34+
ans[level] = ans[level] || [];
35+
ans[level].push(cur.val);
36+
cur.left && queues.push(cur.left);
37+
cur.right && queues.push(cur.right);
38+
}
39+
level++;
40+
}
41+
return ans[ans.length - 1][0];
42+
};
43+
// @lc code=end

0 commit comments

Comments
 (0)