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 16444de

Browse files
update: 103
1 parent 99292bc commit 16444de

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
6262
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [JavaScript](./src/same-tree/res.js) | Easy |
6363
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [JavaScript](./src/symmetric-tree/res.js) | Easy |
6464
| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [JavaScript](./src/binary-tree-level-order-traversal/res.js) | Medium |
65+
| 103 | [binary-tree-zigzag-level-order-traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | [TypeScript](./src/binary-tree-zigzag-level-order-traversal/res.ts) | Medium |
6566
| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [JavaScript](./src/maximum-depth-of-binary-tree/res.js) | Easy |
6667
| 107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | [JavaScript](./src/binary-tree-level-order-traversal-ii/res.js) | Easy |
6768
| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [JavaScript](./src/convert-sorted-array-to-binary-search-tree/res.js) | Easy |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
*/
4+
class TreeNode {
5+
val: number
6+
left: TreeNode | null
7+
right: TreeNode | null
8+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
9+
this.val = (val===undefined ? 0 : val)
10+
this.left = (left===undefined ? null : left)
11+
this.right = (right===undefined ? null : right)
12+
}
13+
}
14+
15+
function zigzagLevelOrder(root: TreeNode | null): number[][] {
16+
const result = [];
17+
let queues = [root];
18+
let reverse = true;
19+
20+
if (!root) {
21+
return result;
22+
}
23+
24+
while (queues.length) {
25+
const currentLevel = queues.slice();
26+
const currentLevelList = [];
27+
queues = [];
28+
reverse = !reverse;
29+
30+
for (let i = 0; i < currentLevel.length; i++) {
31+
const currentNode = currentLevel[i];
32+
currentLevelList.push(currentNode.val);
33+
34+
if (currentNode.left) {
35+
queues.push(currentNode.left);
36+
}
37+
if (currentNode.right) {
38+
queues.push(currentNode.right);
39+
}
40+
}
41+
42+
result.push(reverse ? currentLevelList.reverse() : currentLevelList);
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
(0)

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