-
Notifications
You must be signed in to change notification settings - Fork 0
105. Construct Binary Tree from Preorder and Inorder Traversal #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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]); | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのコメントをご参照ください。 |
||
|
|
||
| TreeNode* build( | ||
| const vector<int>& preorder, | ||
| int preL, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowerCamel と lower_snake が混ざっている点に違和感を感じました。 lower_snake に統一するとよいと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね、統一するほうがいいと思います。 |
||
| int preR, | ||
| int inL | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この preR, 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); | ||
| } | ||
| }; | ||
| 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); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
事前にイテレータから添字に変換しておくと、ここら辺のコードが読みやすくなると思います。
条件式やコンストラクタの中で変換すると少し難しくなると思います
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントありがとうございます。そうですね、条件式やコンストラクタの中で変換するのは煩雑になってしまいそうです。