-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_from_preorder_and_postorder.cpp
53 lines (45 loc) · 1.83 KB
/
binary_tree_from_preorder_and_postorder.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
51
52
53
/**
* 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) {
int preStart = 0;;
return buildTree(preorder, inorder, preStart, 0, inorder.size() -1);
}
// So this recursive function will find where the current value in order ( via preorder is )
// Then it will recursively add what is to it's left and right in the in order array based on that
TreeNode * buildTree(vector<int>& preorder, vector<int>& inorder, int & preStart, int inStart, int inEnd)
{
// If we have nothing in either array, we return null
if(preStart >= preorder.size() || inStart > inEnd)
{
return nullptr;
}
// We make our tree based on the value in order ( preoder ).
TreeNode * node = new TreeNode(preorder[preStart]);
// Once we've made that, we need to find where it lies in order depending on our direction
int orderedIndex;
for (int i = inStart; i <= inEnd; i++)
{
if(inorder[i] == node->val)
{
orderedIndex = i;
// Nothing else to see.
break;
}
}
// Since we've found the corresponding index for this preordered value, we can move to the next
preStart++;
// Here we will recursively call this function for the left & right hand side ( based on index)
node->left = buildTree(preorder, inorder, preStart, inStart, orderedIndex-1);
node->right = buildTree(preorder, inorder, preStart, orderedIndex +1, inEnd);
return node;
}
};