-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_tree.cpp
More file actions
89 lines (69 loc) · 963 Bytes
/
binary_tree.cpp
File metadata and controls
89 lines (69 loc) · 963 Bytes
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
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("arbore.in");
struct nod{
int nr;
nod *st, *dr;
};
nod *r;
int t;
void creare(nod *&r){
int x;
f>>x;
if(x == 0){
r = NULL;
}
else{
r = new nod;
r->nr = x;
creare(r->st);
creare(r->dr);
}
}
void RSD(nod *r){ // pre
if(r != NULL){
cout<<r->nr<<" ";
RSD(r->st);
RSD(r->dr);
}
}
void SRD(nod *r){ // in
if(r != NULL){
SRD(r->st);
cout<<r->nr<<" ";
SRD(r->dr);
}
}
void SDR(nod *r){ // post
if(r != NULL){
SDR(r->st);
SDR(r->dr);
cout<<r->nr<<" ";
}
}
void afis_noduri_de_pe_k(nod *r, int k, int c=1){
if(r){
if(c+1 == k){
cout<<r->st->nr<<" "<<r->dr->nr;
}
else{
afis_noduri_de_pe_k(r, k, c+1);
}
}
}
int main(){
creare(r);
/*
cout<<"RSD: ";
RSD(r);
cout<<"\nSRD: ";
SRD(r);
cout<<"\nSDR: ";
SDR(r);
*/
//cout<<contine_cif(425, 2);
//cout<<produs(r);
afis_noduri_de_pe_k(r, 2);
f.close();
}