/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型二维数组 */ function levelOrder(root) { // write code here const result = []; const traverse = (node, layer) => { if (node !== null) { traverse(node.left, layer + 1); result[layer] ? result[layer].push(node.val) : (result[layer] = [node.val]); traverse(node.right, layer + 1); } }; traverse(root, 0); return result; } module.exports = { levelOrder: levelOrder, };