Skip to content

102 binary tree level order traversal - #26

Open
MA-yo-TA wants to merge 3 commits into
mainfrom
102-Binary-Tree-Level-Order-Traversal
Open

102 binary tree level order traversal#26
MA-yo-TA wants to merge 3 commits into
mainfrom
102-Binary-Tree-Level-Order-Traversal

Conversation

@MA-yo-TA

Copy link
Copy Markdown
Owner


ノード数 n に対して時間計算量 O(n)、空間計算量 O(n)

ノード数の上限は 2000 であり各ノードについて val の取得、子の None 判定とリストへの append などを行う程度なので、計算回数は `2000 * 10 = 2 * 10 ^ 4`くらい、計算時間は 数十 ms くらいか。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment on lines +21 to +22
- 「計算量 Time: O(V + E) = O(V), Space: O(width) = O(V)」
- 自分では全部のノードを見るから O(V)、と雑に考えていたが、実際には高さの回数ループが回り、その中で同じ高さのノードの数だけループが回るという構造であることに気をつける必要がある

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分もわりとO(V)と大雑把に考えがちなので,勉強になりました.

return []

nodes: list[TreeNode] = [root]
traversal: list[list[int]] = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

traversal という変数名はすでに言及されている通り,すこし違和感がありましたが,後続で改善されていますね

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 root is None:
return []

nodes = [root]

@skypenguins skypenguins Jul 20, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら命名として nodespending_nodes とかにします。

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.

ありがとうございます。どういうノードかを明確に表現するのもわかりやすいですね。

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