๋ฌธ์
5639๋ฒ: ์ด์ง ๊ฒ์ ํธ๋ฆฌ
ํธ๋ฆฌ๋ฅผ ์ ์ ์ํํ ๊ฒฐ๊ณผ๊ฐ ์ฃผ์ด์ง๋ค. ๋ ธ๋์ ๋ค์ด์๋ ํค์ ๊ฐ์ 106๋ณด๋ค ์์ ์์ ์ ์์ด๋ค. ๋ชจ๋ ๊ฐ์ ํ ์ค์ ํ๋์ฉ ์ฃผ์ด์ง๋ฉฐ, ๋ ธ๋์ ์๋ 10,000๊ฐ ์ดํ์ด๋ค. ๊ฐ์ ํค๋ฅผ ๊ฐ์ง๋ ๋ ธ๋๋ ์๋ค
www.acmicpc.net
์ฝ๋
#include <iostream>
#include <vector>
using namespace std;
vector<int>v;
void postorder(int start, int end){
if(start>end) return;
if(start==end){
cout<<v[start]<<'\n';
return;
}
int root_index = start;
while(true){
root_index++;
if(v[start]<v[root_index]) break;
if(root_index>end) break;
}
postorder(start+1, root_index-1);
postorder(root_index, end);
cout<<v[start]<<'\n'; // ๋ฃจํธ๋
ธ๋ ์ถ๋ ฅ
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int inp;
while(cin>>inp){
v.push_back(inp);
}
postorder(0, v.size()-1);
}
ํ์ด
์ด์งํธ๋ฆฌ๋ฅผ ์ ์ ์ํํ ๊ฒฐ๊ณผ๊ฐ ์ฃผ์ด์ก์ ๋, ์ด ํธ๋ฆฌ๋ฅผ ํ์ ์ํํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์ ์ ๋๋ค.
ํ์ด ์์ด๋์ด๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ๋ฌธ์ ์ ์์ ์ ๋ ฅ๊ณผ ๊ฐ์ ๋ค์ ์ ๋ ฅ ์์ ๋, ๊ฐ๊ฐ Root, Left, Right๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
50 30 24 5 28 45 98 52 60
๋นจ๊ฐ์: Root, ํ๋์: Left, ์ด๋ก์: Right
๊ทธ ์ด์ ๋ ์ผ๋จ Root ๋ ธ๋๋ 50์ผ ์ด๊ณ , 30๋ถํฐ Left ๋ ธ๋์ธ๋ฐ Root๋ณด๋ค ์ฒ์์ผ๋ก ํฐ ๊ฐ์ด ๋์ค๋ฉด ํด๋น Node๋ Right ๋ ธ๋์ ์์์ ์ด ๋ฉ๋๋ค.
๋ฐ๋ผ์ ํด๋น ์์ด๋์ด ๋ฐฉ์์ผ๋ก ๋ฃจํธ๋ฅผ ๊ธฐ์ ์ผ๋ก ์ข์ฐ ์์ชฝ์ผ๋ก ๋ถํ ํ์ฌ ๋๋ค์ ์ฒ์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ ๋ฐ๋ณตํ๊ฒ ๋๋ฉด ๋ฆฌํ ๋ ธ๋์ ๋๋ฌํ๊ฒ ๋๋๋ฐ ๊ทธ๋ ๋ฆฌํ ๋ ธ๋๋ฅผ ์ถ๋ ฅํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ฉด ํด๊ฒฐํ ์ ์์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 14675๋ฒ - ๋จ์ ์ ๊ณผ ๋จ์ ์ (0) | 2022.06.30 |
---|---|
[๋ฐฑ์ค,c++] 6416๋ฒ - ํธ๋ฆฌ์ธ๊ฐ? (0) | 2022.06.30 |
[๋ฐฑ์ค,c++] 1068๋ฒ - ํธ๋ฆฌ (0) | 2022.06.21 |
[๋ฐฑ์ค,c++] 9934๋ฒ - ์์ ์ด์ง ํธ๋ฆฌ (0) | 2022.06.21 |
[๋ฐฑ์ค,c++] 1655๋ฒ - ๊ฐ์ด๋ฐ๋ฅผ ๋งํด์ (0) | 2022.06.21 |
[๋ฐฑ์ค,c++] 12764๋ฒ - ์ธ์ง๋ฐฉ์ ๊ฐ ์คํ (0) | 2022.06.20 |
๋๊ธ