Skip to content

Commit

Permalink
536 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
ksaveljev committed Jun 21, 2018
1 parent 02e81d7 commit 12bd0ab
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions 536.cpp
@@ -0,0 +1,60 @@
#include <iostream>
#include <string>
using namespace std;

typedef struct node_ {
char c;
struct node_* left;
struct node_* right;
} node;

string inorder, preorder;
int pidx;

int find_node(int start, int end, char c) {
for (int i = start; i <= end; i++) {
if (inorder[i] == c)
return i;
}

return -1;
}

node* rebuild_tree(int start, int end) {
if (start > end) return NULL;

node* r = new node;
r->c = preorder[pidx++];
r->left = NULL;
r->right = NULL;

if (start == end)
return r;

int pos = find_node(start, end, r->c);

r->left = rebuild_tree(start, pos-1);
r->right = rebuild_tree(pos+1, end);

return r;
}

void print_postorder(node* r) {
if (r == NULL) return;

print_postorder(r->left);
print_postorder(r->right);

cout << r->c;
}

int main(void) {
while (cin >> preorder >> inorder) {
pidx = 0;
node* r = rebuild_tree(0, inorder.length()-1);
print_postorder(r);
cout << endl;
}

return 0;
}

0 comments on commit 12bd0ab

Please sign in to comment.