-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105.construct-binary-tree-from-preorder-and-inorder-traversal.cpp
121 lines (116 loc) · 3.78 KB
/
105.construct-binary-tree-from-preorder-and-inorder-traversal.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* @lc app=leetcode id=105 lang=cpp
*
* [105] Construct Binary Tree from Preorder and Inorder Traversal
*
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
*
* algorithms
* Medium (51.05%)
* Likes: 4711
* Dislikes: 122
* Total Accepted: 457.6K
* Total Submissions: 885.9K
* Testcase Example: '[3,9,20,15,7]\n[9,3,15,20,7]'
*
* 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
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* buildTree1(vector<int>& preorder, vector<int>& inorder) {
int preSize = preorder.size();
int inSize = inorder.size();
return core(preorder, 0, preSize - 1, inorder, 0, inSize - 1);
}
TreeNode* core(vector<int>& preorder, int preLeft, int preRight, vector<int>& inorder, int inLeft, int inRight) {
if (preLeft > preRight || inLeft > inRight)
return nullptr;
int rootVal = preorder[preLeft];
int splitInd;
for (splitInd = inLeft; splitInd <= inRight; ++splitInd) {
if (inorder[splitInd] == rootVal)
break;
}
int preLeftTree = preLeft + splitInd - inLeft;
TreeNode* root = new TreeNode(rootVal);
root->left = core(preorder, preLeft + 1, preLeftTree, inorder, inLeft, splitInd - 1);
root->right = core(preorder, preLeftTree + 1, preRight, inorder, splitInd + 1, inRight);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (!preorder.size())
return nullptr;
stack<TreeNode*> st;
int inorderIndex = 0;
TreeNode* root = new TreeNode(preorder[0]);
st.push(root);
for (int i = 1; i < preorder.size(); ++i) {
int preorderVal = preorder[i];
TreeNode* node = st.top();
if (node->val != inorder[inorderIndex]) {
node->left = new TreeNode(preorderVal);
st.push(node->left);
} else {
while (!st.empty() && st.top()->val == inorder[inorderIndex]) {
node = st.top();
st.pop();
inorderIndex++;
}
node->right = new TreeNode(preorderVal);
st.push(node->right);
}
}
return root;
}
unordered_map<int, int> indexMap;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
for (int i = 0; i < inorder.size(); ++i)
indexMap[inorder[i]] = i;
return dfs(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
}
TreeNode* dfs(vector<int>& preorder, vector<int>& inorder, int preL, int preR, int inoL, int inoR)
{
if (inoL > inoR)
return nullptr;
int root = preorder[preL];
TreeNode* node = new TreeNode(root);
int mid = indexMap[root];
int num = mid - inoL;
int preSubR = preL + num;
node->left = dfs(preorder, inorder, preL + 1, preSubR, inoL, mid - 1);
node->right = dfs(preorder, inorder, preSubR + 1, preR, mid + 1, inoR);
return node;
}
};
// @lc code=end