From e9c91e78998bde1a123865df30aca4071bba8159 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Mon, 31 May 2021 10:55:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95=20javaScript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 80 +++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index dca5d3e3..f4091ad5 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -374,6 +374,86 @@ func postorderTraversal(root *TreeNode) []int { } ``` +javaScript: + +> 前序遍历统一迭代法 + +```js + +// 前序遍历:中左右 +// 压栈顺序:右左中 + +var preorderTraversal = function(root, res = []) { + const stack = []; + if (root) stack.push(root); + while(stack.length) { + const node = stack.pop(); + if(!node) { + res.push(stack.pop().val); + continue; + } + if (node.right) stack.push(node.right); // 右 + if (node.left) stack.push(node.left); // 左 + stack.push(node); // 中 + stack.push(null); + }; + return res; +}; + +``` + +> 中序遍历统一迭代法 + +```js + +// 中序遍历:左中右 +// 压栈顺序:右中左 + +var inorderTraversal = function(root, res = []) { + const stack = []; + if (root) stack.push(root); + while(stack.length) { + const node = stack.pop(); + if(!node) { + res.push(stack.pop().val); + continue; + } + if (node.right) stack.push(node.right); // 右 + stack.push(node); // 中 + stack.push(null); + if (node.left) stack.push(node.left); // 左 + }; + return res; +}; + +``` + +> 后序遍历统一迭代法 + +```js + +// 后续遍历:左右中 +// 压栈顺序:中右左 + +var postorderTraversal = function(root, res = []) { + const stack = []; + if (root) stack.push(root); + while(stack.length) { + const node = stack.pop(); + if(!node) { + res.push(stack.pop().val); + continue; + } + stack.push(node); // 中 + stack.push(null); + if (node.right) stack.push(node.right); // 右 + if (node.left) stack.push(node.left); // 左 + }; + return res; +}; + +``` + ----------------------- From 18a37ec17d2e596d12f37373b0decffded380f76 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Mon, 31 May 2021 15:25:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=20=E4=B8=80=E5=A5=97?= =?UTF-8?q?=E6=89=93=E5=85=AB=E4=B8=AAJavaScript=20=E8=BF=AD=E4=BB=A3=20+?= =?UTF-8?q?=20=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) From 4a5c0cb75b1c265f2dacfb3826dccdb3f0bdae98 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Mon, 31 May 2021 17:19:02 +0800 Subject: [PATCH 3/3] update: stack to queue --- problems/0102.二叉树的层序遍历.md | 48 +++++++++++------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 2be38bc4..51bd8510 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1224,15 +1224,15 @@ var rightSideView = function(root) { // 迭代 var averageOfLevels = function(root) { - const stack = [], res = []; - root && stack.push(root); - while(len = stack.length) { + const queue = [], res = []; + root && queue.push(root); + while(len = queue.length) { let sum = 0, l = len; while(l--) { - const {val, left, right} = stack.shift(); + const {val, left, right} = queue.shift(); sum += val; - left && stack.push(left); - right && stack.push(right); + left && queue.push(left); + right && queue.push(right); } res.push(sum/len); } @@ -1263,15 +1263,15 @@ var averageOfLevels = function(root) { // 迭代 const MIN_G = Number.MIN_SAFE_INTEGER; var largestValues = function(root) { - const stack = [], res = []; - root && stack.push(root); - while(len = stack.length) { + const queue = [], res = []; + root && queue.push(root); + while(len = queue.length) { let max = MIN_G; while(len--) { - const {val, left, right} = stack.shift(); + const {val, left, right} = queue.shift(); max = max > val ? max : val; - left && stack.push(left); - right && stack.push(right); + left && queue.push(left); + right && queue.push(right); } res.push(max); } @@ -1300,15 +1300,15 @@ var largestValues = function(root) { // 迭代 var levelOrder = function(root) { - const stack = [], res = []; - root && stack.push(root); - while(len = stack.length) { + const queue = [], res = []; + root && queue.push(root); + while(len = queue.length) { const vals = []; while(len--) { - const {val, children} = stack.shift(); + const {val, children} = queue.shift(); vals.push(val); for(const e of children) { - stack.push(e); + queue.push(e); } } res.push(vals); @@ -1341,15 +1341,15 @@ var levelOrder = function(root) { // 迭代 var connect = function(root) { - const stack = []; - root && stack.push(root); - while(len = stack.length) { + const queue = []; + root && queue.push(root); + while(len = queue.length) { while(len--) { - const node1 = stack.shift(), - node2 = len ? stack[0] : null; + const node1 = queue.shift(), + node2 = len ? queue[0] : null; node1.next = node2; - node1.left && stack.push(node1.left); - node1.right && stack.push(node1.right); + node1.left && queue.push(node1.left); + node1.right && queue.push(node1.right); } } return root;