We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 54eeb57 commit 3b74d61Copy full SHA for 3b74d61
0589-n-ary-tree-preorder-traversal/0589-n-ary-tree-preorder-traversal.cpp
@@ -0,0 +1,35 @@
1
+/*
2
+// Definition for a Node.
3
+class Node {
4
+public:
5
+ int val;
6
+ vector<Node*> children;
7
+
8
+ Node() {}
9
10
+ Node(int _val) {
11
+ val = _val;
12
+ }
13
14
+ Node(int _val, vector<Node*> _children) {
15
16
+ children = _children;
17
18
+};
19
+*/
20
21
+class Solution {
22
23
+ vector<int> preorder(Node* root) {
24
+ vector<int> ans;
25
26
+ function<void(Node*)> dfs = [&](Node* root) {
27
+ if (!root) return;
28
+ ans.push_back(root->val);
29
+ for (auto& i : root->children) dfs(i);
30
+ };
31
32
+ dfs(root);
33
+ return ans;
34
35
0 commit comments