-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_105_Binary_tree_from_preOrder_inOrder.cpp
46 lines (37 loc) · 1.26 KB
/
LeetCode_105_Binary_tree_from_preOrder_inOrder.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
/*
105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder ) {
size_t size = preorder.size();
return buildTree( preorder, 0, size - 1, inorder, 0, size - 1 );
}
TreeNode* buildTree( vector<int>& preorder, int pstart, int pend,
vector<int>& inorder, int istart, int iend ) {
if( pend < pstart || iend < istart ) return NULL;
// first node in pre-order is the root
TreeNode *node = new TreeNode( preorder[pstart] );
// search for root in in-order to know the size of left and right sub-tree
int index = istart;
while( inorder[index] != preorder[pstart] )
index++;
node->left = buildTree( preorder, pstart + 1, pstart + ( index - istart ),
inorder, istart, index - 1 );
node->right = buildTree( preorder, pstart + 1 + ( index - istart ), pend,
inorder, index + 1 , iend );
return node;
}
};