Skip to content

Commit 747f202

Browse files
authored
Update 103-binary-tree-zigzag-level-order-traversal.js
1 parent 9bb7a48 commit 747f202

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

103-binary-tree-zigzag-level-order-traversal.js

+31
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,34 @@ const zigzagLevelOrder = function (root) {
7575

7676
return zigzag;
7777
};
78+
79+
// another
80+
81+
/**
82+
* Definition for a binary tree node.
83+
* function TreeNode(val, left, right) {
84+
* this.val = (val===undefined ? 0 : val)
85+
* this.left = (left===undefined ? null : left)
86+
* this.right = (right===undefined ? null : right)
87+
* }
88+
*/
89+
/**
90+
* @param {TreeNode} root
91+
* @return {number[][]}
92+
*/
93+
const zigzagLevelOrder = function (root) {
94+
const res = []
95+
dfs(root, res, 0)
96+
return res
97+
98+
function dfs(node, res, level) {
99+
if(node == null) return
100+
if(res.length <= level) res.push([])
101+
const tmp = res[level]
102+
if(level % 2 === 0) tmp.push(node.val)
103+
else tmp.unshift(node.val)
104+
105+
dfs(node.left, res, level + 1)
106+
dfs(node.right, res, level + 1)
107+
}
108+
};

0 commit comments

Comments
 (0)