-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindPath.py
37 lines (33 loc) · 1.04 KB
/
FindPath.py
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
28
29
30
31
32
33
34
35
36
37
from copy import deepcopy
class TreeNode():
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution():
def findPath(self, root, exceptNumber):
if root == None:
return None
result = []
path = []
result = self.dfs(root, result, path, exceptNumber)
return result
def dfs(self, root, result, path, exceptNumber):
# 没有下一个子节点
path.append(root.val)
if root.left == None and root.right == None and sum(path) == exceptNumber:
result.append(deepcopy(path))
if root.left != None:
self.dfs(root.left, result, path, exceptNumber)
if root.right != None:
self.dfs(root.right, result, path, exceptNumber)
path.pop()
return result
if __name__ == "__main__":
pRoot = TreeNode(1)
pRoot.left = TreeNode(2)
pRoot.right = TreeNode(3)
exceptNumber = 4
solution = Solution()
result = solution.findPath(pRoot, exceptNumber)
print(result)