-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_path.py
100 lines (94 loc) · 2.94 KB
/
tree_path.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
92
93
94
95
96
97
98
99
100
#coding=utf-8
"""
257. Binary Tree Paths
"""
# 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):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
children = [root.left, root.right]
if not any(children):
return [str(root.val)]
result = []
for child in children:
if child:
result += self.binaryTreePaths(child)
return [str(root.val) + '->' + value for value in result]
def hasPathSum(self, root, sum):
"""
112. Path Sum
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False
if not root.left and not root.right:
return root.val == sum
if self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val):
return True
return False
def hasPathSum1(self, root, sum):
if not root:
return False
stack = []
stack.append((root, sum))
while stack:
top, _sum = stack.pop()
if not top.left and not top.right and top.val == _sum:
return True
if top.right:
stack.append((top.right, _sum-top.val))
if top.left:
stack.append((top.left, _sum-top.val))
return False
def pathSum(self, root, sum):
"""
113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
:param root:
:param sum:
:return:
"""
if not root:
return []
children = [root.left, root.right]
if not any(children):
if root.val == sum:
return [[root.val]]
return []
path = []
if root.left:
left_path = self.pathSum(root.left, sum-root.val)
if left_path:
path.extend([[root.val] + p for p in left_path])
if root.right:
right_path = self.pathSum(root.right, sum-root.val)
if right_path:
path.extend([[root.val] + p for p in right_path])
return path
def pathSum1(self, root, sum):
"""
113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
:param root:
:param sum:
:return:
"""
if not root:
return []
children = [root.left, root.right]
if not any(children) and root.val == sum:
return [[root.val]]
tmp = self.pathSum1(root.left, sum-root.val) + self.pathSum1(root.right, sum-root.val)
return [[root.val] + p for p in tmp]