Created
March 4, 2019 06:56
-
-
Save panyan928/a94d57fea729442d11ba49f4e588f5dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution(object): | |
paths = list() | |
path = list() | |
def getAllpath(self, root, sum1): | |
if not root: | |
return | |
if sum(self.path) + root.val == sum1: | |
self.paths.append(self.path + [root.val]) | |
self.path.append(root.val) | |
self.getAllpath(root.left, sum1) | |
self.getAllpath(root.right, sum1) | |
self.path.pop(-1) | |
def fromRoot(self, root, sum): | |
if root is None: | |
return | |
self.getAllpath(root, sum) | |
self.path = list() | |
self.fromRoot(root.left, sum) | |
self.fromRoot(root.right, sum) | |
def pathSum(self, root, sum): | |
""" | |
:type root: TreeNode | |
:type sum: int | |
:rtype: int | |
""" | |
if root is None: | |
return 0 | |
self.fromRoot(root, sum) | |
return len(self.paths) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment