From f51412e3d43b6bda140913d63f3575585d4ac981 Mon Sep 17 00:00:00 2001 From: xll <18574553598@163.com> Date: Wed, 26 May 2021 20:59:05 +0800 Subject: [PATCH] =?UTF-8?q?0257.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md=E2=80=98=20git=20commit?= =?UTF-8?q?=20-m=200257.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E8=B7=AF=E5=BE=84.=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 964bd7fd..fa9991de 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -351,6 +351,30 @@ class Solution: ``` Go: +JavaScript: +递归版本 +```javascript +var binaryTreePaths = function(root) { + //递归遍历+递归三部曲 + let res=[]; + //1. 确定递归函数 函数参数 + const getPath=function(node,curPath){ + //2. 确定终止条件,到叶子节点就终止 + if(node.left===null&&node.right===null){ + curPath+=node.val; + res.push(curPath); + return ; + } + //3. 确定单层递归逻辑 + curPath+=node.val+'->'; + node.left&&getPath(node.left,curPath); + node.right&&getPath(node.right,curPath); + } + getPath(root,''); + return res; +}; +``` +