-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-reConstructBinaryTree.cpp
executable file
·105 lines (89 loc) · 2.53 KB
/
05-reConstructBinaryTree.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/********************************
*@file:
*@author: Pan HU
*@data: 2015-8-16
*@version: 0.1
*@describe:
********************************/
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
//Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in)
{
if(pre.empty() || in.empty() || pre.size() != in.size())
{
return nullptr;
}
typedef vector<int>::const_iterator vintconit;
vintconit preBegin = pre.begin();
vintconit preEnd = pre.end()-1;
vintconit inBegin = in.begin();
vintconit inEnd = in.end()-1;
struct TreeNode *root = new struct TreeNode(*preBegin);
root->left = root->right = nullptr;
if(preBegin == preEnd)
{//如果前序和后序都只有一个节点,那么这两个节点应该相等,即这棵树只有一个根节点
if(inBegin == inEnd && *preBegin == *preEnd)
{
return root;
}
else
{
throw runtime_error("input error");
//abort();
}
}
//find root node in inorder
ptrdiff_t leftLen = 0;
vintconit rootOfIn = inBegin;
while(rootOfIn != inEnd+1 && *rootOfIn != *preBegin)
{
++leftLen;
++rootOfIn;
}
if(rootOfIn == inEnd+1)
{
throw runtime_error("find root failed");
//abort();
}
vintconit preLeftEnd = preBegin + leftLen;
if(preLeftEnd > preBegin && leftLen > 0)
{//left tree not null
vector<int> leftpre(preBegin+1,preLeftEnd+1);
vector<int> leftin(inBegin,rootOfIn);
root->left = reConstructBinaryTree(leftpre,leftin);
}
if(preLeftEnd < preEnd)
{//right tree not null
vector<int> rightpre(preLeftEnd+1,preEnd+1);
vector<int> rightin(rootOfIn+1,inEnd+1);
root->right = reConstructBinaryTree(rightpre,rightin);
}
return root;
}
int main()
{
int pre[] = {1,2,4,7,3,5,6,8};
int in[] = {4,7,2,1,5,3,8,6};
vector<int> preorder(pre, pre+sizeof(pre)/sizeof(*pre));
vector<int> inorder (in, in+sizeof(in)/sizeof(*in));
TreeNode * root = nullptr;
try{
root = reConstructBinaryTree(preorder,inorder);
} catch (const runtime_error &e){
cerr<<e.what()<<endl;
}
if(root != nullptr)
cout<<root->val<<endl;
else
cout<<"!!!"<<endl;
return 0;
}