Home N-ary Tree Preorder Traversal
Post
Cancel

N-ary Tree Preorder Traversal

Leetcode Problem

N-ary Tree Preorder Traversal

tree가 주어졌을 때, preorder 수행 결과를 구하는 문제입니다.

1
2
3
class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        return [root.val] + sum([self.preorder(x) for x in root.children], []) if root else []

sum 함수로 이중 배열을 flatten하게 바꿀 수 있다는 것을 배웠습니다.





참고

This post is licensed under CC BY 4.0 by the author.