Skip to content

111. Minimum Depth of Binary Tree#22

Open
tarinaihitori wants to merge 2 commits intomainfrom
111-minimum-depth-of-binary-tree
Open

111. Minimum Depth of Binary Tree#22
tarinaihitori wants to merge 2 commits intomainfrom
111-minimum-depth-of-binary-tree

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

自分でもその方式で実装する。
階層ごとに処理する今回の方法の場合、各階層でやりたい処理が追加である場合に対応しやすいが
タプルで管理する方法は階層の深さを明示しなくても常に今の深さが分かる点がある。
今回だと深さを知りたいだけなので、趣味の範囲かなと思った。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

趣味の範囲なんですが、個人的な意見として、queue の中身のデータの整合性が取れているということは、読んでいる人からすると全部読み終わらないと分からないからです。書いている人は分かるわけですが。
つまり、一つの変数に、2つの違う種類のものを入れておいて、その境界を個数で管理しているわけですよね。

リスト2つ使って this_level, next_level を入れ替えていくという方法もあります。

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.

たしかに読み手からすると全部読み終わらないと分からないですね。

リスト2つ使って this_level, next_level を入れ替えていくという方法もあります。

こちらもstep4で書いてみます。

```

スタックを用いた再帰
これだと全部の葉を調べる必要があるので、これをするなら最初の BFS を用いたコードを書くなあ。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

スタックを用いたDFSですね
スタックDFSも再帰DFSもどちらも全ノードを探索することになると思います

return 0

depth = 1
queue = deque([root])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

すでに指摘されていますが、自分もここはqueueを使う必要がない点が気になりました

Comment on lines +34 to +35
if root.left is None and root.right is None:
return 1
Copy link
Copy Markdown

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.

4 participants