-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp.cpp
More file actions
109 lines (105 loc) · 1.58 KB
/
Copy pathp.cpp
File metadata and controls
109 lines (105 loc) · 1.58 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int value)
{
data = value;
left = nullptr;
right = nullptr;
}
};
void preorder(Node *root)
{
if (root)
{
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void it(Node *root)
{
if (!root)
return;
stack<Node *> ans;
while (root or !ans.empty())
{
while (root)
{
cout << root->data << " ";
ans.push(root);
root = root->left;
}
root = ans.top();
ans.pop();
root = root->right;
}
}
void rightview(Node *root)
{
vector<int> view;
if (!root)
return;
vector<int> count;
queue<Node *> s;
s.push(root);
s.push(nullptr);
view.push_back(root->data);
while (!s.empty())
{
Node *cuur = s.front();
s.pop();
if (!cuur)
{
if (count.size())
view.push_back(count.back());
s.push(nullptr);
count.clear();
if (s.empty())
break;
continue;
}
if (cuur->left)
{
s.push(cuur->left);
count.push_back(cuur->left->data);
}
if (cuur->right)
{
s.push(cuur->right);
count.push_back(cuur->right->data);
}
}
for (auto i : view)
cout << i << " ";
}
void leveorder(Node* root){
queue<Node*>a;
a.push(root);
while(!a.empty()){
Node*temp = a.front();
a.pop();
cout<<temp->data<<" ";
if(temp->left)a.push(temp->left);
if(temp->right)a.push(temp->right);
}
}
int main()
{
Node *d = new Node(1);
d->left = new Node(2);
d->right = new Node(3);
d->left->right = new Node(4);
d->left->left = new Node(5);
// preorder(d);
cout << '\n';
// it(d);
// rightview(d);
leveorder(d);
return 0;
}