-
Notifications
You must be signed in to change notification settings - Fork 0
/
25二叉树中和为某一值的路径.py
39 lines (32 loc) · 974 Bytes
/
25二叉树中和为某一值的路径.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
38
39
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def __init__(self):
self.onePath = []
self.PathArray = []
def FindPath(self, root, expectNumber):
if root is None:
return self.PathArray
self.onePath.append(root.val)
expectNumber -= root.val
if expectNumber==0 and not root.left and not root.right:
self.PathArray.append(self.onePath[:])
elif expectNumber>0:
self.FindPath(root.left,expectNumber)
self.FindPath(root.right,expectNumber)
expectNumber += root.val
self.onePath.pop()
return self.PathArray
root = TreeNode(10)
root.left = TreeNode(5)
root.right = TreeNode(12)
left = root.left
left.left = TreeNode(4)
left.right = TreeNode(7)
test = Solution()
res = test.FindPath(root,22)
print(res)