From 8c77bb2565658fb19c3d1159988ff6c50804876b Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Wed, 19 May 2021 18:45:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86javascript?= =?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 | 37 +++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 937ef603..2d571294 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -226,7 +226,7 @@ func PreorderTraversal(root *TreeNode) (res []int) { var traversal func(node *TreeNode) traversal = func(node *TreeNode) { if node == nil { - return + return } res = append(res,node.Val) traversal(node.Left) @@ -272,6 +272,41 @@ func PostorderTraversal(root *TreeNode) (res []int) { } ``` +javaScript: + +```js + +前序遍历: + +var preorderTraversal = function(root, res = []) { + if (!root) return res; + res.push(root.val); + preorderTraversal(root.left, res) + preorderTraversal(root.right, res) + return res; +}; + +中序遍历: + +var inorderTraversal = function(root, res = []) { + if (!root) return res; + inorderTraversal(root.left, res); + res.push(root.val); + inorderTraversal(root.right, res); + return res; +}; + +后序遍历: + +var postorderTraversal = function(root, res = []) { + if (!root) return res; + postorderTraversal(root.left, res); + postorderTraversal(root.right, res); + res.push(root.val); + return res; +}; +``` +