@@ -39,70 +39,3 @@ function bfs(row, res) {
39
39
bfs ( next , res )
40
40
}
41
41
42
- // another
43
-
44
- /**
45
- * Definition for a binary tree node.
46
- * function TreeNode(val, left, right) {
47
- * this.val = (val===undefined ? 0 : val)
48
- * this.left = (left===undefined ? null : left)
49
- * this.right = (right===undefined ? null : right)
50
- * }
51
- */
52
- /**
53
- * @param {TreeNode } root
54
- * @return {number[][] }
55
- */
56
- const zigzagLevelOrder = function ( root ) {
57
- if ( ! root ) return [ ] ;
58
- const queue = [ root ] ;
59
- const zigzag = [ ] ;
60
- let numLevels = 1 ;
61
- while ( queue . length > 0 ) {
62
- const width = queue . length ;
63
- const levelTraversal = [ ] ;
64
- for ( let i = 0 ; i < width ; i ++ ) {
65
- const currentNode = queue . shift ( ) ;
66
- if ( currentNode . right ) queue . push ( currentNode . right ) ;
67
- if ( currentNode . left ) queue . push ( currentNode . left ) ;
68
- numLevels % 2 === 0
69
- ? levelTraversal . push ( currentNode . val )
70
- : levelTraversal . unshift ( currentNode . val ) ;
71
- }
72
- zigzag . push ( levelTraversal ) ;
73
- numLevels ++ ;
74
- }
75
-
76
- return zigzag ;
77
- } ;
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