-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path106_ConstructBinaryTreefromInorderandPostorderTraversal.cpp
55 lines (49 loc) · 1.77 KB
/
106_ConstructBinaryTreefromInorderandPostorderTraversal.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
#include "stdafx.h"
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *_left, TreeNode *_right) : val(x), left(_left), right(_right) {}
};
//-----------------------------------------------------------------
#include <algorithm>
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
if (inorder.empty()) return nullptr;
return build(&postorder[0], &postorder[0] + postorder.size(), &inorder[0], &inorder[0] + inorder.size());
}
private:
TreeNode* build(const int *postBegin, const int *postEnd, const int *inBegin, const int *inEnd) {
if (postBegin == postEnd) return nullptr;
const int *inMid = find(inBegin, inEnd, postEnd[-1]);
int leftCount = inMid - inBegin;
auto node = new TreeNode(postEnd[-1]);
node->left = build(postBegin, postBegin + leftCount, inBegin, inMid);
node->right = build(postBegin + leftCount, postEnd - 1, inMid + 1, inEnd);
return node;
}
};
//-----------------------------------------------------------------
static void printTree(TreeNode *node) {
if (node == nullptr) return;
printTree(node->left);
cout << node->val << ',';
printTree(node->right);
}
int main() {
Solution so;
vector<int> orders[][2] = {
{vector<int>{}, vector<int>{}},
{ vector<int>{1}, vector<int>{1} },
{ vector<int>{2, 1}, vector<int>{1, 2} },
{ vector<int>{1, 2}, vector<int>{2, 1} },
{ vector<int>{2, 1, 3,}, vector<int>{1, 2, 3} },
{ vector<int>{3, 2, 1, }, vector<int>{1, 2, 3} },
};
for (auto &v2 : orders) {
printTree(so.buildTree(v2[0], v2[1]));
cout << endl;
}
}