-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConstruct_Binary_Tree_from_Inorder_and_Postorder_Traversal.cpp
57 lines (51 loc) · 2.04 KB
/
Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.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
/*
Given inorder and postorder traversal of a tree, construct the binary tree.
Note
You may assume that duplicates do not exist in the tree.
*/
#include <vector>
#include "lintcode.h"
using namespace std;
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
*@param inorder : A list of integers that inorder traversal of a tree
*@param postorder : A list of integers that postorder traversal of a tree
*@return : Root of a tree
*/
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
// write your code here
return buildTreeRecursion(inorder, postorder , 0, inorder.size() - 1, 0, postorder.size() - 1);
}
TreeNode* buildTreeRecursion(vector<int>& inorder, vector<int>& postorder, int inorderBegin, int inorderEnd, int postorderBegin, int postorderEnd) {
if (inorderBegin < 0 || inorderEnd > inorder.size() - 1 || postorderBegin < 0 || postorderEnd > postorder.size() - 1 || inorderBegin > inorderEnd || postorderBegin > postorderEnd) {
return NULL;
}
int rootNode = postorder[postorderEnd];
TreeNode* result = new TreeNode(rootNode);
if (postorderBegin == postorderEnd) {
return result;
}
int leftChildLength = inorderBegin;
while (inorder[leftChildLength] != rootNode) {
leftChildLength++;
}
leftChildLength = leftChildLength - inorderBegin;
int rightChildLength = inorderEnd - inorderBegin + 1 - leftChildLength - 1;
result->left = buildTreeRecursion(inorder, postorder, inorderBegin, inorderBegin + leftChildLength - 1, postorderBegin, postorderBegin + leftChildLength - 1);
result->right = buildTreeRecursion(inorder, postorder, inorderEnd - rightChildLength + 1, inorderEnd, postorderEnd - 1 - rightChildLength + 1, postorderEnd - 1);
return result;
}
};