From 20922e2f074d6d97b16f9ebffdc45f5fa5f03b3e Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Sun, 16 May 2021 14:09:37 +0200 Subject: [PATCH] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 27 +++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 73387257..591ba81b 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -324,8 +324,31 @@ class Solution { ``` Python: +```Python +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def binaryTreePaths(self, root: TreeNode) -> List[str]: + path=[] + res=[] + def backtrace(root, path): + if not root:return + path.append(root.val) + if (not root.left)and (not root.right): + res.append(path[:]) + ways=[] + if root.left:ways.append(root.left) + if root.right:ways.append(root.right) + for way in ways: + backtrace(way,path) + path.pop() + backtrace(root,path) + return ["->".join(list(map(str,i))) for i in res] - +``` Go: @@ -335,4 +358,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -