Skip to content

Commit 3b74d61

Browse files
committed
Time: 15 ms (37.51%), Space: 15.1 MB (67.41%) - LeetHub
1 parent 54eeb57 commit 3b74d61

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
val = _val;
16+
children = _children;
17+
}
18+
};
19+
*/
20+
21+
class Solution {
22+
public:
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

Comments
 (0)