-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path106-中序后序构造二叉树.cpp
46 lines (46 loc) · 1.52 KB
/
106-中序后序构造二叉树.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size()==0 || postorder.size()==0 || inorder.size()!=postorder.size() ) //序列不存在或者两个序列的大小不一样,返回null
return NULL;
vector<int> in_left,in_right,post_left,post_right; //左右子树的中序、后序遍历
int root_index = 0; //根节点的下标
int len = inorder.size(); //序列长度
TreeNode * root = new TreeNode(postorder[len-1]); //找到根节点
int i;
//找到中序的根节点下标
for(i=0;i<len;i++)
{
if(inorder[i] == postorder[len-1])
{
root_index = i;
break;
}
}
//左的中序、后序遍历(除去根节点)
for(i=0;i<root_index;i++)
{
in_left.push_back(inorder[i]);
post_left.push_back(postorder[i]);
}
//右的中序、后序遍历(除去根节点)
for(i=root_index+1;i<len;i++)
{
in_right.push_back(inorder[i]);
post_right.push_back(postorder[i-1]);
}
//递归调用
root->left = buildTree(in_left,post_left);
root->right = buildTree(in_right,post_right);
return root;
}
};