-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_2.py
More file actions
52 lines (41 loc) · 950 Bytes
/
question_2.py
File metadata and controls
52 lines (41 loc) · 950 Bytes
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/8/16 15:42
# @Author : cancan
# @File : question_2.py
# @Function : N叉树的后序遍历
"""
Question:
给定一个N叉树,返回其节点值的后序遍历。
Example:
例如,给定一个 3叉树 :
1
/ | \
3 2 4
/ \
5 6
返回其后序遍历: [5,6,3,2,4,1].
Note:
递归法很简单,你可以使用迭代法完成此题吗?
"""
"""
# 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]
"""
result = []
def f(node, data):
if not node: return
for item in node.children:
f(item, data)
data.append(node.val)
f(root, result)
return result