forked from wangcy6/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
105. 从前序与中序遍历序列构造二叉树.cpp
50 lines (40 loc) · 1.52 KB
/
105. 从前序与中序遍历序列构造二叉树.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
/**
* https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
* 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>& preorder, vector<int>& inorder) {
//数据合法性检验
if (preorder.size() != inorder.size()) return NULL;
//递归构造一颗树
return helper(preorder, 0,preorder.size()-1, \
inorder,0,inorder.size()-1);
}
TreeNode* helper(vector<int>& preorder,int pStart,int pEnd,vector<int>& inorder,int iStart,int iEnd)
{ //cout<< pEnd << " "<< pStart << " " <<iEnd << iStart<<endl;
if (pEnd < pStart || iEnd < iStart)
{
return NULL;
}
TreeNode* root =new TreeNode(preorder[pStart]);
//根据preorder[pStart]拆分左右子树,并且计算范围
int mid=iStart ;
// while (inorder[iStart] != root->val) //iStart 位置不能发生变化,变化的mid
while (inorder[mid] != root->val)
{
mid++;
}
int left = mid-iStart;
//对各自范围构造一棵树
root->left = helper(preorder,pStart+1,pStart+left,inorder,iStart,mid-1);
root->right = helper(preorder,pStart+left+1,pEnd,inorder,mid+1,iEnd);
return root;
}
};