广度优先搜索
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
if(!root) return []
let result = []
let que = []
que.push(root)
while(que.length !== 0) {
let init_size = que.length
let temp = []
for (let i = 0; i<init_size; i++) {
let mp = que.shift()
if(mp.left) que.push(mp.left)
if(mp.right) que.push(mp.right)
temp.push(mp.val)
}
result.push(temp)
}
return result
};