aboutsummaryrefslogtreecommitdiff
path: root/other/n_ary_tree_traverse.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/n_ary_tree_traverse.cpp')
-rw-r--r--other/n_ary_tree_traverse.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/other/n_ary_tree_traverse.cpp b/other/n_ary_tree_traverse.cpp
new file mode 100644
index 0000000..9c89682
--- /dev/null
+++ b/other/n_ary_tree_traverse.cpp
@@ -0,0 +1,25 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ vvd levelOrder(Node *root) {
+ vvd 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;
+ }
+};