-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105.cpp
33 lines (33 loc) · 1.06 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
/**
* 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 {
// Runtime: 20 ms, faster than 79.16% of C++ online submissions for Construct
// Binary Tree from Preorder and Inorder Traversal. Memory Usage: 19.2 MB,
// less than 25.65% of C++ online submissions for Construct Binary Tree from
// Preorder and Inorder Traversal.
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if (!preorder.size())
return nullptr;
using itr = vector<int>::iterator;
function<TreeNode *(itr, itr, itr &)> build =
[&build](itr first, itr last, itr &t) -> TreeNode * {
auto it = find(first, last, *t);
if (it == last)
return nullptr;
TreeNode *root = new TreeNode(*t++);
root->left = build(first, it, t);
root->right = build(it + 1, last, t);
return root;
};
auto it = begin(preorder);
return build(begin(inorder), end(inorder), it);
}
};