-
Notifications
You must be signed in to change notification settings - Fork 0
/
nary-tree-postorder-traversal.py
64 lines (49 loc) · 1.39 KB
/
nary-tree-postorder-traversal.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
# Given an n-ary tree, return the postorder traversal of its nodes' values.
# For example, given a 3-ary tree:
# 1
# 3 2 4
# 5 6
# Return its postorder traversal as: [5,6,3,2,4,1].
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
#if(root is None):
# return
if(len(root.children)==0):
return [root.val]
else:
result=[]
for i in root.children:
#print(root.val)
result =result+ (self.postorder(i))
result.append(root.val)
return result
def createRoot(data):
# Read data of children and val
# Call Create root for each children
val=data["val"]
children=[]
for item in data["children"]:
children.append(createRoot(item))
return Node(val,children)
s=Solution()
#print(s.postorder(None))
a=createRoot({"$id":"1",
"children":[
{"$id":"2","children":[
{"$id":"5","children":[],"val":5},
{"$id":"6","children":[],"val":6}],
"val":3},
{"$id":"3","children":[],"val":2},
{"$id":"4","children":[],"val":4}
],
"val":1})
print(s.postorder(a))