给定一个二叉树,返回该二叉树层序遍历的结果,(从左到右,一层一层地遍历)
例如:
给定的二叉树是{3,9,20,#,#,15,7},
该二叉树层序遍历的结果是
[
[3],
[9,20],
[15,7]
]
提示:
0 <= 二叉树的结点数 <= 1500
function levelOrder( root ) { // write code here if(root==null) return [[]]; let queue = []; let res = []; queue.push(root); while(queue.length>0){ let len = queue.length; let arr=[]; while(len--){ let node = queue.shift(); arr.push(node.val); if(node.left) queue.push(node.left); if(node.right) queue.push(node.right); } if(arr.length) res.push(arr); } return res; }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return int整型二维数组 */ function levelOrder( root ) { // write code here //{1,2,3,4,#,#,5} //第一步,为空返回空list var list = []; if(!root){ return list; } //第二步,定义结果集,root压入list var res=[]; list.push(root); //第三步,list长度>0 while(list.length>0){ //3-1 临时的数组存每层的节点值 let arr=[]; //3-2 定义len 等于list长度 let len = list.length;//list的长度 //3-3 终止条件 while(len){ //删除第一个节点,并返回第一个节点 let node = list.shift(); //临时数组存储节点 arr.push(node.val); if(node.left){//存在左子树,左子树压入list list.push(node.left); } if(node.right){//存在右子树,右子树压入list list.push(node.right); } //len 长度对应减1 len--; } //每层节点急压入结果集 res.push(arr); } //最后,return 结果 return res; } module.exports = { levelOrder : levelOrder };
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return int整型二维数组 */ function levelOrder( root ) { // write code here if(!root) return []; let queue = [root]; // 建立queue,将跟元素放进去 let twoArr = [] while(queue.length) { let oneArr = []; let queueLength = queue.length; while(queueLength) { let node = queue.shift(); // 去掉根节点,此时queue里的元素就是下一层的所有节点 node.left && queue.push(node.left); // 找根节点的左右两个子节点 node.right && queue.push(node.right); queueLength--; oneArr.push(node.val); // 将结果存到一个一维向量里 } twoArr.push(oneArr); // 再把这个一维向量存到二维向量里 } return twoArr; } module.exports = { levelOrder : levelOrder };
function levelOrder( root ) { // write code here let levelList = [] if(!root) return levelList const linkList = [] levelList.push(root) while(levelList.length){ const currentArr = [], currentLevelList = [] for(let index =0;index<levelList.length;index++){ currentArr.push(levelList[index].val) if(levelList[index].left){ currentLevelList.push(levelList[index].left) } if(levelList[index].right){ currentLevelList.push(levelList[index].right) } } levelList = currentLevelList linkList.push(currentArr) } return linkList } module.exports = { levelOrder : levelOrder };把二叉树数据看成层级划分,从顶层开始依次从左到右顺序记录每层数据,将数据遍历即可得到结果
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return int整型二维数组 */ function levelOrder( root ) { if(!root) { return []; } let levelList = [root]; let result = []; while(levelList.length) { let temp = []; let length = levelList.length; for(let i = 0; i < length; i++) { let arr = levelList.shift(); temp.push(arr.val); if(arr.left) levelList.push(arr.left); if(arr.right) levelList.push(arr.right); } result.push(temp); } return result; } module.exports = { levelOrder : levelOrder };
// 使用队列的先进先出结构 function levelOrder( root ) { if(!root) return []; let res = [];//存放结果的数组 let queue = [root]; while(queue.length) { let subRes = [];//每一层的子数组 let len = queue.length; for(let i=0; i<len; i++) { let temp = queue.shift(); subRes.push(temp.val); if(temp.left) queue.push(temp.left); if(temp.right) queue.push(temp.right); } res.push(subRes); } return res; } module.exports = { levelOrder : levelOrder };
function levelOrder( root ) { const res = []; if (!root) return res; const queue = [root]; while (queue.length > 0) { res.push(queue.map(i => i.val)); let len = queue.length; while (len) { const cur = queue.shift(); if (cur.left) queue.push(cur.left); if (cur.right) queue.push(cur.right); len--; } } return res; }
function levelOrder( root ) { // write code here const res =[] const traversal = (root,depth) =>{ if(root !== null){ if(!res[depth]){ res[depth] = [] } traversal(root.left, depth+1) res[depth].push(root.val) traversal(root.right,depth+1) } } traversal(root,0) return res }
function levelOrder( root ) { // write code here let levels = [] if (!root) return levels function walk (node, level) { if (levels.length === level){ levels.push([]) } levels[level].push(node.val) if (node.left) walk(node.left, level+1) if (node.right) walk(node.right, level+1) } walk(root, 0) return levels }