GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-1/algorithms/python/PathSumII/pathSum.py at master · mtangm/leetcode-1 Expand file tree/ Copy pathpathSum.pyMore file actions 27 lines (24 loc) · 756 Bytes / Copy pathpathSum.pyFile metadata and controls27 lines (24 loc) · 756 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 """ Method 1 """ def pathSum(self, root, sum): if not root: return [] res = [] def dfs(root, sum, ls, res): if not root.left and not root.right and sum == root.val: ls.append(root.val) res.append(ls) if root.left: dfs(root.left, sum - root.val, ls + [root.val], res) if root.right: dfs(root.right, sum - root.val, ls + [root.val], res) dfs(root, sum, [], res) return res """ Method 2 """ def pathSum(self, root, sum): if not root: return [] if not root.left and not root.right and root.val == sum: return [[root.val]] temp = self.pathSum(root.left, sum - root.val) + self.pathSum(root.right, sum - root.val) return [[root.val] + i for i in temp] |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |