From b9a988f3fd154b4f277d9c8580724768ff2ca1a0 Mon Sep 17 00:00:00 2001 From: mrhou <349909605@qq.com> Date: Fri, 3 Aug 2018 00:01:49 -0400 Subject: [PATCH] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E5=92=8C=E4=B8=BA=E6=9F=90=E4=B8=80=E5=80=BC=E7=9A=84=E8=B7=AF?= =?UTF-8?q?=E5=BE=84.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一些小修改 --- ...74\347\232\204\350\267\257\345\276\204.py" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/\345\211\221\346\214\207offer/\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.py" "b/\345\211\221\346\214\207offer/\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.py" index bc5bb6e..fde1840 100644 --- "a/\345\211\221\346\214\207offer/\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.py" +++ "b/\345\211\221\346\214\207offer/\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.py" @@ -50,3 +50,39 @@ def FindPath2(root, path, currentNum): FindPath2(root, [], 0) return result + + +''' +another solution based your code +''' +class Solution: + # 返回二维列表,内部每个列表表示找到的路径 + def FindPath(self, root, expectNumber): + # write code here + if not root: + return [] + result = [] + path = [] + currentNum = 0 + n = expectNumber + self.FindPath2(root, result, path, currentNum, n) + return result + def FindPath2(self, root, result, path, currentNum, n): + currentNum += root.val + # root使用append转成了列表,因为最后要一个序列,root本身还是树结构 + path.append(root) + # 判断是不是到叶子节点了,到叶子节点了就要判断值的和是不是相等 + flag = root.left == None and root.right == None + # 返回值是一个等于整数的序列 + if currentNum == n and flag: + onepath = [] + for node in path: + onepath.append(node.val) + result.append(onepath) + if currentNum < n: + if root.left: + self.FindPath2(root.left, result, path, currentNum, n) + if root.right: + self.FindPath2(root.right, result, path, currentNum, n) + # 拿到一个正确的路径后要弹出,回到上一次父节点继续递归 + path.pop()