Skip to content

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

Merged
Ryotaro25 merged 3 commits intomainfrom
problem29
Jun 12, 2025
Merged

105. Construct Binary Tree from Preorder and Inorder Traversal#31
Ryotaro25 merged 3 commits intomainfrom
problem29

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
arai60ではないですが以前、赤黒木を実装している際に知識の補強として教えていただいた問題です。
https://leetcode.com/problems/binary-search-tree-iterator/description/

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp
hash_map.cppとstep4.cpp(step3の非破壊的変更ver)とmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

}

private:
int preorder_index = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Solutionインスタンスが作られ、そのインスタンスでbuildTree()が複数回呼ばれた場合を考えるとpreorder_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.

たとえば、HTML Parser だったら、HTML 一つにつきインスタンス一つという設計も自然でしょう。呼ぶ側の気持ちになってどう実装するかを考えるということかと思います。

通常、プログラムというのは、誰かに使って欲しいと思って書かれていて、つまり、それがエンジニアリングをしているということです。ライブラリーの場合は、それは呼ぶ相手のためにエンジニアリングをしているはずです。

class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (!inorder.empty()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if (inorder.empty()) {
  return nullptr;
}
...

の形の方がコードを読みながら考えることが減って読みやすいかなと思います

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.

@fhiyo
hash_map_step2にて実装しました。
ネストが浅くなる分こちらの方が読みやすいですね。

改めてみてみると最下部にreturn nullptrがあるのは読みづらいですね。

Comment on lines +20 to +21
TreeNode* BuildTreeHelper(const vector<int>& preorder, int preorder_start, int preorder_end,
const vector<int>& inorder, int inorder_start, int inorder_end) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

preorder_end - preorder_start == inorder_end - inorder_start という制約があるので、これをtree_sizeとかの名前にして、preorder, inorder, preorder_start, inorder_start, tree_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.

@fhiyo

レビューありがとうございます。
step6に実装してみました。少しスッキリしました。

int value = preorder[preorder_start];
TreeNode* node = new TreeNode(value);

int position = FindSeparatingPosition(inorder, inorder_start, inorder_end, value);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

positionという変数名が曖昧かなと思いました。
これはinorderにおける根を表すindexだと思うので、inorder_root_indexとかでしょうか

あと、indexをいじるのは認知不可が上がるので、inorder, preorderそれぞれのsliceを引き回すようにすると読みやすくなると思いました

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.

@Yoshiki-Iwasa
レビューありがとうございます。inorder_root_index使わせていただきました。

indexをいじるのは認知不可が上がるので、inorder, preorderそれぞれのsliceを引き回すようにすると読みやすくなると思いました

今回複数パターン書いてみましたが、indexを触るverは理解するのが辛かったです。

forループでindexを見つけてからiteratorに変換していたがfind関数が存在していた


## 他の解法
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

preorderとinorderを使って順に木を構築する方法もあります。

ご参考:https://github.com/kazukiii/leetcode/pull/30/files#diff-0d121db22136b97146c44a3926ea99756ef36fd61caf75dd9d91b5d5a4a484f5

@Ryotaro25 Ryotaro25 merged commit efb0483 into main Jun 12, 2025
1 check passed
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.

5 participants