https://leetcode.com/problems/n-ary-tree-preorder-traversal/

 

N-ary Tree Preorder Traversal - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

방금 전 푼 문제랑 똑~같이 풀면 된다.

 

class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        answer = []
        
        stack = [root]
        while stack:
            node = stack.pop()

            if node:
                answer.append(node.val)
                for child in node.children[::-1]:
                    stack.append(child)
        
        return answer
                    

 

+ Recent posts