/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode}root * @return {number[][]} */ var levelOrder = function(root) { var res = []
var levelTransal = function (node, order) { res[order] = res[order] || [] res[order].push(node.val)
if (node.left) { levelTransal(node.left, order + 1) } if (node.right) { levelTransal(node.right, order + 1) } } if (!root) return [] levelTransal(root, 0)