Skip to content

Commit 0e210c5

Browse files
authored
Update 1702 - Tree Traversals.cpp
1 parent 0b78d14 commit 0e210c5

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/1702 - Tree Traversals.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ Author: Sachin Srivastava (mrsac7)
55
*/
66
#include<bits/stdc++.h>
77
using namespace std;
8-
8+
99
#define int long long
1010
#define endl '\n'
11-
11+
1212
vector<int> pre, in;
1313
int a[100005];
14-
void post(int l1, int h1, int l2, int h2) {
15-
if (l1 > h1)
14+
int x = 0, n;
15+
void post(int l = 0, int h = n-1) {
16+
if (l > h) {
17+
x--;
1618
return;
17-
int i = a[pre[l1]];
18-
post(l1+1, l1+1+i-l2-1, l2, i);
19-
post(l1+1+i-l2, h1, i+1, h2);
19+
}
20+
int i = a[pre[x]];
21+
++x;
22+
post(l, i-1);
23+
++x;
24+
post(i+1, h);
2025
cout<<in[i]<<' ';
2126
}
2227
signed main(){
@@ -26,7 +31,7 @@ signed main(){
2631
freopen("output.txt", "w", stdout);
2732
#endif
2833

29-
int n; cin>>n;
34+
cin>>n;
3035
for (int i = 0; i < n; i++) {
3136
int x; cin>>x;
3237
pre.push_back(x);
@@ -36,5 +41,5 @@ signed main(){
3641
in.push_back(x);
3742
a[x] = i;
3843
}
39-
post(0,n-1,0,n-1);
44+
post();
4045
}

0 commit comments

Comments
 (0)