From f0964bd1eb0471566d1068ef577e984b79aeb350 Mon Sep 17 00:00:00 2001 From: xll <18574553598@163.com> Date: Fri, 21 May 2021 11:16:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82?= =?UTF-8?q?=E5=BA=8F=E9=81=8D=E5=8E=86javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 149 ++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 1cb4164f..c40f4e2a 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -79,6 +79,35 @@ public: return result; } }; +``` +javascript代码: + +```javascript +var levelOrder = function(root) { + //二叉树的层序遍历 + let res=[],queue=[]; + queue.push(root); + if(root===null){ + return res; + } + while(queue.length!==0){ + // 记录当前层级节点数 + let length=queue.length; + //存放每一层的节点 + let curLevel=[]; + for(let i=0;inode.val?max:node.val; + node.left&&queue.push(node.left); + node.right&&queue.push(node.right); + } + //把每一层的最大值放到res数组 + res.push(max); + } + return res; +}; +``` ## 116.填充每个节点的下一个右侧节点指针