-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path589-n-ary-tree-preorder-traversal_WIP.cpp
80 lines (64 loc) · 1.76 KB
/
589-n-ary-tree-preorder-traversal_WIP.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cstddef>
#include <vector>
#include <algorithm>
// performance measure header
#include <iomanip>
#include <chrono>
// https://leetcode.com/problems/n-ary-tree-preorder-traversal/
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:
ListNode* insert(ListNode* head, int data) {
if (head == NULL)
head = new ListNode(data);
else {
ListNode* tail {head};
while (tail && tail->next != NULL)
tail = tail->next;
tail->next = new ListNode(data);
}
return head;
}
Node* create
vector<int> preorder(Node* root) {
}
};
int main() {
int levels, first_val, val, children;
std::cout << "Insert the number of levels of the tree: ";
std::cin >> levels;
std::cout << "Insert the value of the first node of the tree: ";
std::cin >> first_val;
std::cout << "Insert the number of children: ";
std::cin >> children;
while (--levels > 1) {
}
ListNode* head {NULL};
Solution mylist;
int T, data;
cout << "Insert nodes of the first list\n";
cin >> T;
while (T-- > 0) {
cin >> data;
head = mylist.insert(head, data);
}
// performance measure
auto t1 {chrono::high_resolution_clock::now()};
ListNode* headReversed {mylist.reverseList(head)};
auto t2 {chrono::high_resolution_clock::now()};
chrono::duration<double, milli> ms_double {t2 - t1};
cout << setprecision(17) << ms_double.count() << " ms\n";
mylist.display(headReversed);
cout << '\n';
return 0;
}