Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

なんとか自力で実装。preorderの一番左は必ずrootになることをベースに、DFSしながらpreorder,inorderを共に分割していきながら木を構築したが添え字管理がかなり煩雑になった。

### step2

煩雑なindex管理をChatGPTに直してもらった。

### step3

3回通すまで書き直し。
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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:
void traverse(
TreeNode* tree,
const vector<int>& preorder,
const vector<int>& inorder,
int pre_left,
int pre_right,
int in_left,
int in_right
) {
auto it = find(inorder.begin() + in_left, inorder.begin() + in_right, preorder[pre_left]);
if (it - inorder.begin() != in_left) {
tree->left = new TreeNode(preorder[pre_left + 1]);
traverse(
tree->left,
preorder,
inorder,
pre_left + 1,
pre_left + it - (inorder.begin() + in_left),
in_left,
it - inorder.begin() - 1
);
}
if (it - inorder.begin() != in_right) {
tree->right = new TreeNode(preorder[pre_left + it - (inorder.begin() + in_left) + 1]);
Comment on lines +36 to +37
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

事前にイテレータから添字に変換しておくと、ここら辺のコードが読みやすくなると思います。
条件式やコンストラクタの中で変換すると少し難しくなると思います

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。そうですね、条件式やコンストラクタの中で変換するのは煩雑になってしまいそうです。

traverse(
tree->right,
preorder,
inorder,
pre_left + it - (inorder.begin() + in_left) + 1,
pre_right,
it - inorder.begin() + 1,
in_right
);
}
}

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
TreeNode* tree = new TreeNode(preorder[0]);
int size = preorder.size();
traverse(tree, preorder, inorder, 0, size - 1, 0, size - 1);
return tree;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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:
unordered_map<int, int> inorder_val_to_index;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
5ky7/arai60#22 (comment)


TreeNode* build(
const vector<int>& preorder,
int preL,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lowerCamel と lower_snake が混ざっている点に違和感を感じました。 lower_snake に統一するとよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね、統一するほうがいいと思います。

int preR,
int inL
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この preR, inL など添字の役割が少し複雑だと感じました。
preorder は単に左から右にインクリメントする添字があれば十分なのですが、preorder の範囲を管理しているのがやらなくても良いことをやっていることになり複雑になっていると思いました。
範囲を管理するのは inorder にした方が、直感的だと思います。

) {
if (preL > preR) {
return nullptr;
}

int rootVal = preorder[preL];
int rootIdx = inorder_val_to_index[rootVal];
int leftSize = rootIdx - inL;

TreeNode* root = new TreeNode(rootVal);

root->left = build(preorder, preL + 1, preL + leftSize, inL);
root->right = build(preorder, preL + leftSize + 1, preR, rootIdx + 1);

return root;
}

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
for (int i = 0; i < inorder.size(); i++) {
inorder_val_to_index[inorder[i]] = i;
}

return build(preorder, 0, preorder.size() - 1, 0);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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:
unordered_map<int, int> inorder_val_to_index;

TreeNode* build(
const vector<int>& preorder,
int preL,
int preR,
int inL
) {
if (preL > preR) {
return nullptr;
}

int rootVal = preorder[preL];
int rootIdx = inorder_val_to_index[rootVal];
int leftSize = rootIdx - inL;

TreeNode* root = new TreeNode(rootVal);

root->left = build(preorder, preL + 1, preL + leftSize, inL);
root->right = build(preorder, preL + leftSize + 1, preR, rootIdx + 1);

return root;
}

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
for (int i = 0; i < inorder.size(); i++) {
inorder_val_to_index[inorder[i]] = i;
}

return build(preorder, 0, preorder.size() - 1, 0);
}
};