Post

leetcode(리트코드)4월20일 challenge589-N-ary Tree Preorder Traversal

leetcode April 20일 - N-ary Tree Preorder Traversal 문제입니다.

1. 문제

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


2. Input , Output


3. 분류 및 난이도

Eazy 난이도입니다.
4월 20일자 챌린지 문제입니다.


4. 문제 해석

  • 이진트리가 아닌 일반 트리가 들어옵니다.
  • 트리를 preorder한 값을 vector에 넣어 반환합니다.

5. code

코드의 핵심은 자식들을 반대로 덱에 넣어줘서 자식이 있는 것을 우선순위로 꺼내는 것입니다.

c++

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
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> preorder(Node* root) {
        deque<Node*> q;
        vector<int> res;
        if(!root) return res;
        q.push_front(root);
        while(!q.empty()){
            Node* frontnode = q.front();
            q.pop_front();
            res.push_back(frontnode->val);
            for(int i = frontnode->children.size()-1 ; i >=0; --i){
                q.push_front(frontnode->children[i]);
            }
        }
        return res;
    }
};

6. 결과 및 후기, 개선점

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