-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.cpp
59 lines (46 loc) Β· 1.06 KB
/
tree.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
class Node {
public:
int data;
Node* left;
Node*right;
Node(int data) {
this->data = data;
left = 0;
right = 0;
}
};
Node* adddata(int data)
{
Node* newNode = new Node(data);
// understand that from constructor anyways left/right pointer becoming 0 - so we need not worry about that
return newNode;
}
void inorder(Node*root, vector<int>&ans) {
if (root == 0) return;
inorder(root->left, ans);
ans.push_back(root->data);
inorder(root->right, ans);
}
int32_t main() {
///////////
// c_p_c();
///////////
_IOS
//////////
Node* root = new Node(10);// root node
// note: Node* node = 0; // 0 can only be assigned - can't assign pointer *x = 100 - it says it would yield datatype errors ~ int can not be converted to node*
root->left = adddata(2);
root->right = adddata(3);
root->left->left = adddata(28);
root->left->right = adddata(25);
root->right->left = adddata(33);
root->right->right = adddata(31);
vector<int> ans;
inorder(root, ans);
for (auto i : ans)
{
cout << i << " ";
}
// cerr << "time: " << clock() << " ms" << '\n';
return 0;
}