-
Notifications
You must be signed in to change notification settings - Fork 980
/
solution.h
28 lines (25 loc) · 950 Bytes
/
solution.h
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
#include <vector>
#include <cstddef>
#include <algorithm>
using std::vector; using std::next; using std::prev;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
using cIter = vector<int>::const_iterator;
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return buildTree(preorder.cbegin(), preorder.cend(), inorder.cbegin(), inorder.cend());
}
TreeNode *buildTree(cIter preBeg, cIter preEnd, cIter inBeg, cIter inEnd) {
if (preBeg >= preEnd || inBeg >= inEnd) return NULL;
TreeNode *root = new TreeNode(*preBeg);
auto inRoot = std::find(inBeg, inEnd, root->val);
root->left = buildTree(next(preBeg), next(preBeg, inRoot-inBeg+1), inBeg, inRoot);
root->right = buildTree(next(preBeg, inRoot-inBeg+1), preEnd, next(inRoot), inEnd);
return root;
}
};