Skip to content

Create 105. Construct Binary Tree from Preorder and Inorder Traversal.md#32

Open
irohafternoon wants to merge 1 commit intomainfrom
105.-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
Open

Create 105. Construct Binary Tree from Preorder and Inorder Traversal.md#32
irohafternoon wants to merge 1 commit intomainfrom
105.-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal

Conversation

@irohafternoon
Copy link
Copy Markdown
Owner

TreeNode* BuildTreeHelper(vector<int>& preorder, int left, int right,
const std::map<int, int>& value_to_inorder_index,
int& preorder_index) {
if (preorder_index >= preorder.size()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが、37行目の考え方と統一して、
if (!(preorder_index < preorder.size()))としたくなりました。

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.

ありがとうござます。おっしゃる通り一貫した記載にするべきでした

if (preorder_index == preorder.size()) {
break;
}
// DFS順に探索するため、stackにright→leftの順番でstackに積む
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

「pre-order順に探索するため」ですかね?

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.

ありがとうございます。in-order順をDFS順と誤って覚えてしまっていました。

nodes_right_unfixed);
nodes_right_unfixed.push(node);
}
return gather_descendants(-1, value_to_preorder_index, nodes_right_unfixed);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

-1の意図を理解するのに手間取りました。
static const int ROOT_PARENT_POSITION = -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.

コメントありがとうございます。いずれにしても、-1とだけ書いてもあまり伝わらないので、コメントか意味を持たせた定数で読み手の理解をスムーズにできるようにします

return gather_descendants(-1, value_to_preorder_index, nodes_right_unfixed);
}
private:
TreeNode* gather_descendants(int node_position,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの問題だと思いますが、ここのnodeはparent_node等の命名をつけても良いかもしれません。

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.

ありがとうございます。
おっしゃる通り、parent以下にまだ右側が確定していないノードを纏めて繋げるという意味の関数ですので、parentをつけた方が意味がわかりやすいと思いました。

nodes_right_unfixed);
nodes_right_unfixed.push(node);
}
return gather_descendants(-1, value_to_preorder_index, nodes_right_unfixed);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には定数に置くよりもコメントで意図を書くほうがいいと思います。全部追い出すということですよね。


```

- inorder順に走査して、preorderの制限を確認しながらnodeを埋めていく方法 https://github.com/tarinaihitori/leetcode/pull/29/files#r2044913447
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この解き方は、このあたりに触発されて書いたものなので、こういうこともできる話半分くらいでいいと思います。
kazukiii/leetcode#30 (comment)
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0

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