-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path105-gusrb3164.cpp
47 lines (43 loc) · 1.21 KB
/
105-gusrb3164.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
/**
* 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:
vector<int> iorder;
vector<int> porder;
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
iorder = inorder;
porder = preorder; // 전역 변수로 재정의
return dfs(0, iorder.size() - 1);
}
TreeNode *dfs(int start, int end)
{
if (start > end || porder.empty())
return nullptr;
int idx = findIdx(porder[0]); //현재 루트값이 inorder에서 위치한 인덱스 저장
porder.erase(porder.begin());
TreeNode *node = new TreeNode(iorder[idx]);
node->left = dfs(start, idx - 1);
node->right = dfs(idx + 1, end);
return node;
}
int findIdx(int num)
{
for (int i = 0; i < iorder.size(); i++)
{
if (iorder[i] == num)
return i;
}
return -1;
}
};