代码仓库:Github | Leetcode solutions @doubleZ0108 from Peking University.
- 解法1(T56% S75%):问题还是树的遍历,只不过换成了N叉树,递归的逻辑跟二叉树是一样的,只是将左右指针换成了循环children数组
class Solution:
def preorder(self, root: 'Node') -> List[int]:
res = []
def deepin(node):
if not node: return
res.append(node.val)
for child in node.children:
deepin(child)
deepin(root)
return res