import { TreeNode } from './BinaryTree' ; param { TreeNode } root return { number [ ] [ ] } function zigzagLevelOrder ( root ) { if ( ! root ) return [ ] ; let result = [ ] , queue = [ root ] ; let even = false ; while ( queue . length ) { let levelSize = queue . length ; let currentLevel = [ ] ; while ( levelSize -- ) { let node = queue . shift ( ) ; currentLevel . push ( node . val ) ; if ( node . left && node . left . val !== null ) queue . push ( node . left ) ; if ( node . right && node . right . val !== null ) queue . push ( node . right ) ; } if ( even ) { result . push ( currentLevel . reverse ( ) ) ; } else { result . push ( currentLevel ) ; } even = ! even ; } return result ; } export { zigzagLevelOrder } ;