105. Construct Binary Tree from Preorder and Inorder Traversal#28
Open
105. Construct Binary Tree from Preorder and Inorder Traversal#28
Conversation
nodchip
reviewed
Oct 26, 2025
| int preorder_index = 0; | ||
| return BuildTreeHelper(preorder,value_to_inorder_index, 0, inorder.size() - 1, preorder_index); | ||
| } | ||
| private: |
There was a problem hiding this comment.
自分なら private: の前に空行を入れ、コードの塊を視覚的に分かりやすし、読み手にとって読みやすくします。人によって判断が分かれるかもしれません。
Google の C++ Style ガイドには入れ過ぎないように、読み手に取ってコードの構造が分かりやすいように入れましょうと書かれているようです。
参考までにスタイルガイドへのリンクを貼ります。
https://google.github.io/styleguide/cppguide.html#Vertical_Whitespace
Use vertical whitespace sparingly; unnecessary blank lines make it harder to see overall code structure. Use blank lines only where they aid the reader in understanding the structure.
上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。
| const map<int, int>& value_to_inorder_index, | ||
| int left, int right, | ||
| int& preorder_index) { | ||
| if (!(preorder_index < preorder.size())) { |
There was a problem hiding this comment.
ここは ! を使うことによって読みやすくなる情報がないため、単に
if (preorder_index => preorder.size()) {もしくは
if (preorder.size() <= preorder_index) {が良いと思います。
| 1. valueとそれがinorderのどこにあるかをmapで対応付ける(value : inorder_index) | ||
| 2. DFS関数を呼ぶ。渡すものは、preorder、区間[left, right]、map, preorderのindex | ||
| 3. preorderのindexがリストのサイズを超えたらnullptrを返す。 | ||
| 4. preorderの値に対応するinorder_indexが[left,right]内になかったらnullptr |
potrue
reviewed
Oct 26, 2025
| int left_tree_size = inorder_index - inorder_start; | ||
| int right_tree_size = size - left_tree_size - 1; | ||
| TreeNode* node = new TreeNode(value); | ||
| ++preorder_start; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
今回の問題
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
次回の問題
https://leetcode.com/problems/longest-increasing-subsequence/