forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
105.cpp
37 lines (32 loc) · 1.17 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
/**
* 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.empty() || inorder.empty() || preorder.size() != inorder.size())
return nullptr;
return buildTree(preorder,0,preorder.size() - 1,inorder,0,inorder.size() - 1);
}
TreeNode* buildTree(vector<int> &preorder,int b1,int e1,vector<int> &inorder,int b2,int e2)
{
if(e1 < b1 || e2 < b2 || e1 - b1 != e2 - b2) return nullptr;
int rootval = preorder[b1];
int count = 0;
for(int i = b2;i <= e2;i++){
if(inorder[i] != rootval) count++;
else break;
}
if(count == e2 - b2 + 1) return nullptr;
TreeNode *root = new TreeNode(rootval);
root->left = buildTree(preorder,b1 + 1,b1 + count,inorder,b2,b2 + count - 1);
root->right = buildTree(preorder,b1 + count + 1,e1,inorder,b2 + count + 1,e2);
return root;
}
};