Skip to content

105. Construct Binary Tree from Preorder and Inorder Traversal#28

Open
hemispherium wants to merge 1 commit into
mainfrom
0105-construct-binary-tree-from-preorder-and-inorder-traversal
Open

105. Construct Binary Tree from Preorder and Inorder Traversal#28
hemispherium wants to merge 1 commit into
mainfrom
0105-construct-binary-tree-from-preorder-and-inorder-traversal

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 21, 2026

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.

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

*/
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)

Comment on lines +36 to +37
if (it - inorder.begin() != in_right) {
tree->right = new TreeNode(preorder[pre_left + it - (inorder.begin() + in_left) + 1]);
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.

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

const vector<int>& preorder,
int preL,
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 にした方が、直感的だと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants