From 18a37ec17d2e596d12f37373b0decffded380f76 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Mon, 31 May 2021 15:25:34 +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=86=20=E4=B8=80=E5=A5=97=E6=89=93?= =?UTF-8?q?=E5=85=AB=E4=B8=AAJavaScript=20=E8=BF=AD=E4=BB=A3=20+=20?= =?UTF-8?q?=E9=80=92=E5=BD=92=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 270 ++++++++++++++++++++++ 1 file changed, 270 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 00607082..2be38bc4 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1100,6 +1100,276 @@ var levelOrder = function (root) { }; ``` +> 二叉树的层序遍历(Javascript语言完全版) (迭代 + 递归) + +```js +/** + * 102. 二叉树的层序遍历 + * @param {TreeNode} root + * @return {number[][]} + */ + +// 迭代 + +var levelOrder = function(root) { + const queue = [], res = []; + root && queue.push(root); + while(len = queue.length) { + const val = []; + while(len--) { + const node = queue.shift(); + val.push(node.val); + node.left && queue.push(node.left); + node.right && queue.push(node.right); + } + res.push(val); + } + return res; +}; + +// 递归 +var levelOrder = function(root) { + const res = []; + function defs (root, i) { + if(!root) return; + if(!res[i]) res[i] = []; + res[i].push(root.val) + root.left && defs(root.left, i + 1); + root.right && defs(root.right, i + 1); + } + defs(root, 0); + return res; +}; + + +/** + * 107. 二叉树的层序遍历 II + * @param {TreeNode} root + * @return {number[][]} + */ + +// 迭代 + +var levelOrderBottom = function(root) { + const queue = [], res = []; + root && queue.push(root); + while(len = queue.length) { + const val = []; + while(len--) { + const node = queue.shift(); + val.push(node.val); + node.left && queue.push(node.left); + node.right && queue.push(node.right); + } + res.push(val); + } + return res.reverse() +}; + +// 递归 + +var levelOrderBottom = function(root) { + const res = []; + function defs (root, i) { + if(!root) return; + if(!res[i]) res[i] = []; + res[i].push(root.val); + root.left && defs(root.left, i + 1); + root.right && defs(root.right, i + 1); + } + defs(root, 0); + return res.reverse(); +}; + +/** + * 199. 二叉树的右视图 + * @param {TreeNode} root + * @return {number[]} + */ + +// 迭代 + +var rightSideView = function(root) { + const res = [], queue = []; + root && queue.push(root); + while(l = queue.length) { + while (l--) { + const {val, left, right} = queue.shift(); + !l && res.push(val); + left && queue.push(left); + right && queue.push(right); + } + } + return res; +}; + +// 递归 +var rightSideView = function(root) { + const res = []; + function defs(root, i) { + if(!root) return; + res[i] = root.val; + root.left && defs(root.left, i + 1); + root.right && defs(root.right, i + 1); + } + defs(root, 0); + return res; +}; + +/** + * 637. 二叉树的层平均值 + * @param {TreeNode} root + * @return {number[]} + */ + +// 迭代 +var averageOfLevels = function(root) { + const stack = [], res = []; + root && stack.push(root); + while(len = stack.length) { + let sum = 0, l = len; + while(l--) { + const {val, left, right} = stack.shift(); + sum += val; + left && stack.push(left); + right && stack.push(right); + } + res.push(sum/len); + } + return res; +}; + +// 递归 +var averageOfLevels = function(root) { + const resCount = [], res = []; + function defs(root, i) { + if(!root) return; + if(isNaN(res[i])) resCount[i] = res[i] = 0; + res[i] += root.val; + resCount[i]++; + root.left && defs(root.left, i + 1); + root.right && defs(root.right, i + 1); + } + defs(root, 0); + return res.map((val, i) => val / resCount[i]); +}; + +/** + * 515. 在每个树行中找最大值 + * @param {TreeNode} root + * @return {number[]} + */ + +// 迭代 +const MIN_G = Number.MIN_SAFE_INTEGER; +var largestValues = function(root) { + const stack = [], res = []; + root && stack.push(root); + while(len = stack.length) { + let max = MIN_G; + while(len--) { + const {val, left, right} = stack.shift(); + max = max > val ? max : val; + left && stack.push(left); + right && stack.push(right); + } + res.push(max); + } + return res; +}; + +// 递归 +var largestValues = function(root) { + const res = []; + function defs (root, i) { + if(!root) return; + if(isNaN(res[i])) res[i] = root.val; + res[i] = res[i] > root.val ? res[i] : root.val; + root.left && defs(root.left, i + 1); + root.right && defs(root.right, i + 1); + } + defs(root, 0); + return res; +}; + +/** + * 429. N 叉树的层序遍历 + * @param {Node|null} root + * @return {number[][]} + */ + +// 迭代 +var levelOrder = function(root) { + const stack = [], res = []; + root && stack.push(root); + while(len = stack.length) { + const vals = []; + while(len--) { + const {val, children} = stack.shift(); + vals.push(val); + for(const e of children) { + stack.push(e); + } + } + res.push(vals); + } + return res; +}; + +// 递归 + +var levelOrder = function(root) { + const res = []; + function defs (root, i) { + if(!root) return; + if(!res[i]) res[i] = []; + res[i].push(root.val); + for(const e of root.children) { + defs(e, i + 1); + } + } + defs(root, 0); + return res; +}; + +/** + * 116. 填充每个节点的下一个右侧节点指针 + * 117. 填充每个节点的下一个右侧节点指针 II + * @param {Node} root + * @return {Node} + */ + +// 迭代 +var connect = function(root) { + const stack = []; + root && stack.push(root); + while(len = stack.length) { + while(len--) { + const node1 = stack.shift(), + node2 = len ? stack[0] : null; + node1.next = node2; + node1.left && stack.push(node1.left); + node1.right && stack.push(node1.right); + } + } + return root; +}; + +// 递归 +var connect = function(root) { + const res = []; + function defs (root, i) { + if(!root) return; + if(res[i]) res[i].next = root; + res[i] = root; + root.left && defs(root.left, i + 1); + root.right && defs(root.right, i + 1); + } + defs(root, 0); + return root; +}; +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)