一棵圣诞树记作根节点为
root
的二叉树,节点值为该位置装饰彩灯的颜色编号。请按照从左到右的顺序返回每一层彩灯编号,每一层的结果记录于一行。示例 1:
输入:root = [8,17,21,18,null,null,6] 输出:[[8],[17,21],[18,6]]提示:
节点总数 <= 1000
标签:150,right,val,II,leetcodeLCR,array,null,root,left From: https://blog.csdn.net/Turboyiyi/article/details/143504481/** * 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 decorateRecord = function(root) { let array = []; getResult(root,0,array); return array; }; var getResult = function(root,k,array) { // K 记录当前遍历到了第几层 if(!root) return null; if(k == array.length) array.push(new Array()); array[k].push(root.val); getResult(root.left,k + 1,array); getResult(root.right,k + 1,array); };