Created
September 8, 2022 14:55
-
-
Save time-river/00c7b529a0c9b6123e817a8dddc5f900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
void get_post_order(const std::string &pre_order, const std::string &in_order) { | |
if (pre_order.length() == 0 && in_order.length() == 0) { | |
return; | |
} else if (pre_order.length() == 1 && in_order.length() == 0) { | |
std::cout << pre_order[0] << ' '; | |
} else if (pre_order.length() == 0 && in_order.length() == 1) { | |
std::cout << in_order[0] << ' '; | |
} else if (pre_order.length() == 1 && in_order.length() == 1) { | |
std::cout << pre_order[0] << ' '; | |
} else { | |
size_t i; | |
char ch = pre_order[0]; | |
for (i = 0; i < in_order.length(); i++) { | |
if (ch == in_order[i]) | |
break; | |
} | |
get_post_order(pre_order.substr(1, i), in_order.substr(0, i)); | |
get_post_order(pre_order.substr(i+1, -1), in_order.substr(i+1, -1)); | |
std::cout << ch << ' '; | |
} | |
} | |
// DGEBHFCA | |
int main(int argc, char *argv[]) { | |
std::string pre_order("ABDEGCFH"); | |
std::string in_order("DBGEACHF"); | |
get_post_order(pre_order, in_order); | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment