aboutsummaryrefslogtreecommitdiff
path: root/n_ary_tree_traverse.cpp
blob: d6cbebd60d33cc8daa3477b0eb880c18304c1cb9 (plain)
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
#include "leetcode.h"

class Solution {
public:
  vvd(int) levelOrder(Node *root) {
    vvd(int) ans;
    if (!root)
      return ans;
    queue<Node *> q;
    q.push(root);
    while (!q.empty()) {
      int n = q.size();
      vector<int> v;
      while (n--) {
        auto curr = q.front();
        q.pop();
        v.push_back(curr->val);
        for (auto it : curr->children)
          q.push(it);
      }
      ans.push_back(v);
    }
    return ans;
  }
};