-
Notifications
You must be signed in to change notification settings - Fork 79
/
23.py
91 lines (70 loc) · 2.26 KB
/
23.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding:utf-8 -*-
import copy
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def cmp(a, b):
if len(a) > len(b):
return -1
else:
return 0
class Solution:
# 返回二维列表,内部每个列表表示找到的路径
def FindPath(self, root, expectNumber):
# write code here
if root == None:
return []
if root.val==expectNumber:
return [[1]]
if root.val>expectNumber:
return []
result = []
a = []
root.val = [root.val]
a.append(root)
while a:
cnode = a[0]
del a[0]
if cnode.left != None:
c = sum(cnode.val) + cnode.left.val
if c < expectNumber:
temp = copy.copy(cnode.left.val)
cnode.left.val = copy.copy(cnode.val)
cnode.left.val.append(temp)
a.append(cnode.left)
elif c > expectNumber:
cnode.left = None
elif cnode.left.left == None and cnode.left.right == None:
t = copy.copy(cnode.val)
t.append(cnode.left.val)
result.append(t)
if cnode.right != None:
c = sum(cnode.val)+ cnode.right.val
if c < expectNumber:
temp = copy.copy(cnode.right.val)
cnode.right.val = copy.copy(cnode.val)
cnode.right.val.append(temp)
a.append(cnode.right)
elif c > expectNumber:
cnode.right = None
elif cnode.right.left == None and cnode.right.right == None:
t = copy.copy(cnode.val)
t.append(cnode.right.val)
result.append(t)
result=sorted(result,cmp )
return result
pRoot8 = TreeNode(10)
treeNode1 = TreeNode(5)
treeNode2 = TreeNode(12)
treeNode3 = TreeNode(4)
treeNode4 = TreeNode(7)
treeNode5 = TreeNode(3)
treeNode6 = TreeNode(10)
# pRoot8.left = treeNode1
# pRoot8.right = treeNode2
# treeNode1.left = treeNode3
# treeNode1.right = treeNode4
s = Solution()
print s.FindPath(pRoot8, 10)