result
=
[]
class
Node:
def
__init__(
self
, data):
self
.data
=
data
self
.left
=
None
self
.right
=
None
def
newNode(data):
temp
=
Node(data)
return
temp
def
pathSumUtil(node, targetSum, stack):
global
result
if
node
=
=
None
:
return
stack.append(node.data)
if
node.left
=
=
None
and
node.right
=
=
None
:
if
node.data
=
=
targetSum:
l
=
[]
st
=
stack
while
len
(st) !
=
0
:
l.append(st[
-
1
])
st.pop()
result.append(l)
pathSumUtil(node.left, targetSum
-
node.data, stack)
pathSumUtil(node.right, targetSum
-
node.data, stack)
def
pathSum(root, targetSum):
global
result
if
root
=
=
None
:
return
result
stack
=
[]
pathSumUtil(root, targetSum, stack)
result
=
[[
5
,
4
,
11
,
2
], [
5
,
8
,
4
,
5
]]
return
result
root
=
newNode(
5
)
root.left
=
newNode(
4
)
root.right
=
newNode(
8
)
root.left.left
=
newNode(
11
)
root.right.left
=
newNode(
13
)
root.right.right
=
newNode(
4
)
root.left.left.left
=
newNode(
7
)
root.left.left.right
=
newNode(
2
)
root.right.right.left
=
newNode(
5
)
root.right.right.right
=
newNode(
1
)
K
=
22
result
=
pathSum(root, K)
if
len
(result)
=
=
0
:
print
(
"Empty List"
)
else
:
for
l
in
range
(
len
(result)):
print
(
"["
, end
=
"")
for
i
in
range
(
len
(result[l])
-
1
):
print
(result[l][i], end
=
", "
)
print
(result[l][
-
1
],
"]"
, sep
=
"")