-
Notifications
You must be signed in to change notification settings - Fork 0
102 binary tree level order traversal medium #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| next_level_nodes.append(node.right) | ||
| level_order_node_vals.append(node_vals) | ||
| nodes = next_level_nodes | ||
| return level_order_node_vals |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
読みやすいです。個人的にはlevel_order_node_valsはresultぐらいでもいいと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
確かに少し冗長ですね。 node_vals_by_level, vals_by_level あたりも良いかなと思っています。
|
|
||
| - Step1では辞書を使っていたが、はじめからリストを用いることで、最後にリストに変換する手間を省いた | ||
| - step1ではpost-order traversalを用いていたが、step2ではpre-order traversalを用いた。 | ||
| - どちらでも動くが、pre-orderの方が「訪れた順に値がリストに入る」という要件に対して、より自然に思える |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
step1.py と step2.py はどちらも pre-order で、本ファイルの上に書かれているのはどちらも post-order ですね。私も pre-order が自然だと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
その通りですね。すこしコードのコピペにミスがあったので、READMEを修正しておきました。
| def search(node: TreeNode | None, level: int) -> None: | ||
| if not node: | ||
| return | ||
| if node.left: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この if はなくても動くでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rootがNoneの場合には動かないと思います。
if not root:
...
とはじめに書けば、search関数の内部ではNoneのチェックが不要になると思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この if とは if node.left: と if node.right: を指していました。書き方として、再帰関数の上部で if not node: return があるので、None の判定の仕事を次のサイクルに任せるパターンもあるというお話でした。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにそれは1つの手ですね、コメントありがとうございます。
| - 例えば、非常に偏った木(例えば、すべてのノードが右の子ノードのみを持つ場合)では、DFSの再帰スタックが`O(n)`になる可能性がある。逆に、完全二分木ではDFSの最大深さは`O(log n)`であり、BFSの方が多くのメモリを消費する。 | ||
|
|
||
| ## システム設計 | ||
| Q. 「このlevelOrder関数を、非常に巨大な木(例えば、数百万ノード)を扱うサービスの一部として利用するシナリオを考えます。クライアント(例: Webフロントエンド)は、一度に全レベルを表示するのではなく、'次のレベルを取得'ボタンでレベルを1つずつ要求します。このような要求に応じて効率的にデータを返すには、現在の実装をどのように変更しますか?メモリ効率を特に重視してください。」 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これは、前提として、巨大な木はサーバーサイドにあるんですよね。
そうすると、「次のレベルを取得」を押したときには、前のプロセスはもう終了していることが多いだろうと思います。そうすると必要なレベルのデータを毎回生成することになるかと思います。完全二分木だとすると、あるレベルの数はその上の数の総和よりも多いですから、(保持よりも再生成のコストのほうが安く)あまり問題にならないでしょうね。
|
LGTMです。 |
問題へのリンク
Binary Tree Level Order Traversal - LeetCode
言語
Python
問題の概要
与えられた二分木の各レベルにおけるノードの値をリストとして返す問題。
自分の解法
step1
ノードの数を
n、木の最大高さをHとすると、O(n)O(H)step2 DFS
計算量:ステップ1に同じ
step3
step4 (FB)
別解・模範解答
BFS(レベルばらばら)
BFS(レベルごと)
ノードの数を
n、木の最大幅をWとすると、O(n)O(W)想定されるフォローアップ質問
CS 基礎
Q.「あなたが実装した反復的なBFSアプローチの空間計算量は、木のどのような特性に依存しますか?また、深さ優先探索(DFS)を用いた再帰的なアプローチと比較した場合、どのような形状の木でBFSがより多くのメモリを消費し、逆にどのような形状の木でDFSがより多くのメモリを消費するか、具体例を挙げて説明してください。」
A.
n//2=O(n)になる。O(n)になる可能性がある。逆に、完全二分木ではDFSの最大深さはO(log n)であり、BFSの方が多くのメモリを消費する。システム設計
Q. 「このlevelOrder関数を、非常に巨大な木(例えば、数百万ノード)を扱うサービスの一部として利用するシナリオを考えます。クライアント(例: Webフロントエンド)は、一度に全レベルを表示するのではなく、'次のレベルを取得'ボタンでレベルを1つずつ要求します。このような要求に応じて効率的にデータを返すには、現在の実装をどのように変更しますか?メモリ効率を特に重視してください。」
その他
次に解く問題の予告