105. Construct Binary Tree from Preorder and Inorder Traversal#29
Open
TakayaShirai wants to merge 1 commit intomainfrom
Open
105. Construct Binary Tree from Preorder and Inorder Traversal#29TakayaShirai wants to merge 1 commit intomainfrom
TakayaShirai wants to merge 1 commit intomainfrom
Conversation
mamo3gr
reviewed
Feb 23, 2026
Comment on lines
+92
to
+102
| int pickInorderRootIndex(List<int> inorder, int rootValue) { | ||
| var rootIndex = 0; | ||
|
|
||
| for (var i = 0; i < inorder.length; i++) { | ||
| if (inorder[i] == rootValue) { | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| throw Exception('inorder does not have the given rootValue.'); | ||
| } |
There was a problem hiding this comment.
ここは list.indexOf() を使うとどうでしょうか。
https://api.dart.dev/dart-core/List/indexOf.html
5ky7
reviewed
Feb 25, 2026
| ); | ||
|
|
||
| root.left = leftTree; | ||
| root.right = rightTree; |
There was a problem hiding this comment.
Dartに詳しくないのですが,以下のようにかけるのならそちらの方がシンプルだと感じました.root.leftという文字列,leftTreeという文字列から得られる情報はほとんど同じなので.
root.left = buildTree(
preorder.sublist(1, leftNodesCount + 1),
inorder.sublist(0, rootIndex),
);
root.right = buildTree(
preorder.sublist(leftNodesCount + 1),
inorder.sublist(rootIndex + 1),
);
Comment on lines
+63
to
+68
| for (var i = 0; i < inorder.length; i++) { | ||
| if (inorder[i] == rootValue) { | ||
| rootIndex = i; | ||
| break; | ||
| } | ||
| } |
| class Solution { | ||
| TreeNode? buildTree(List<int> preorder, List<int> inorder) { | ||
| int pickInorderRootIndex(List<int> inorder, int rootValue) { | ||
| var rootIndex = 0; |
|
全体的に読みやすいです. |
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/description/
次に解く問題:https://leetcode.com/problems/paint-fence/description/