/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return int整型二维数组 */ // function levelOrder( root ) { // // write code here // let res = [] // let arr = [] // arr.push(root) // while(arr.length>0){ // let res1 = [] // let newArr = [] // arr.forEach(v=>{ // res1.push(v.val) // v.left&&newArr.push(v.left) // v.right&&newArr.push(v.right) // }) // arr = newArr // res.push(res1) // } // return res // } function levelOrder(root) { // write code here if(root===null) return [] let res = []; let arr = []; arr.push(root); while (arr.length > 0) { let newArr = []; let row = []; arr.forEach((v) => { row.push(v.val); v.left && newArr.push(v.left); v.right && newArr.push(v.right); }); arr = newArr; res.push(row); } return res; } module.exports = { levelOrder: levelOrder, };