-
Notifications
You must be signed in to change notification settings - Fork 0
104 Maximum Depth of Binary Tree #21
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
Open
MA-yo-TA
wants to merge
3
commits into
main
Choose a base branch
from
104-Maximum-Depth-of-Binary-Tree
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # 104. Maximum Depth of Binary Tree | ||
|
|
||
| https://leetcode.com/problems/maximum-depth-of-binary-tree/ | ||
|
|
||
| ## step1(まず通す) | ||
|
|
||
| 深さ優先探索だと一番深いところにいつ当たるかわからないので、max_depth を持っておいて更新していく。 | ||
|
|
||
| 幅優先探索だと木なら同じ高さのノードを順番に見ていく形になるので最後に見たノードが一番深い。 | ||
|
|
||
| ということで幅優先探索する。いつも (node, distance) のキューでやってたが、今回は階層ごとに見ていって一番深いところの深さを得るというのを素朴に表現してみる。 | ||
|
|
||
| ## step2(整形&他の人のコードを読む) | ||
|
|
||
| - https://github.com/hayashi-ay/leetcode/pull/22/changes | ||
| - Python では再帰が好まれない印象なので避けたが、木のような構造が再帰的なやつは再帰で書くととてもわかりやすい | ||
| - [浅井健一『プログラミングの基礎』(サイエンス社)](https://www.saiensu.co.jp/search/?isbn=978-4-7819-1160-1&y=2007#detail)(ちなみにこんな名前で OCaml。名著。)ではデータ構造の再帰的な定義と一対一で対応した再帰を「構造にしたがった再帰」と呼んでいるが、今回のようなものはまさにそう。 | ||
| - 木の再帰的な定義: | ||
| - None は木である | ||
| - ある一つのノードがあってその子が全て木であるものは木である。 | ||
| - この問題を解く再帰関数: | ||
| - None なら高さは 0 である | ||
| - ある一つのノードにおける高さはその子の高さの max に 1 を加えたものである。 | ||
| - 今回は ノード数 <= 10 ^ 4 なので、再帰の深さは log 10 ^ 4 ~ 13 とかなので Python のリミット的にもスタックのサイズ的にも基本的に問題ない。 | ||
|
|
||
| ## step3(10分以内にさっとかける \* 3回) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| nodes = [root] | ||
| next_layer = [] | ||
| depth = 0 | ||
| while nodes: | ||
| depth += 1 | ||
| for node in nodes: | ||
| if node.left is not None: | ||
| next_layer.append(node.left) | ||
| if node.right is not None: | ||
| next_layer.append(node.right) | ||
| nodes = next_layer | ||
| next_layer = [] | ||
|
|
||
| return depth |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
| return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| nodes = [root] | ||
| next_layer = [] | ||
| depth = 0 | ||
| while nodes: | ||
| depth += 1 | ||
| for node in nodes: | ||
| if node.left is not None: | ||
| next_layer.append(node.left) | ||
| if node.right is not None: | ||
| next_layer.append(node.right) | ||
| nodes = next_layer | ||
| next_layer = [] | ||
|
|
||
| return depth | ||
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.
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.
next_layerの初期化はまとめられそうです。
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.
ありがとうございます。
個人的には
nodesとnext_layerの初期化・更新はセットでやりたい感覚(今の階層 vs. 次の階層、という対の意識)があってこう書いたのですが h-masder さんの感覚だとどうですか?nodes はループ中も一貫した意味を持つ名前なのに対して next_layer は while のループ一回ごとに完結して意味を持つのでループ内だけに存在してて欲しい(からまとめる)というのもわかります。
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.
私としては、next_layer はループの中で毎回作るものというイメージがあるので、中で初期化しています。
とはいえ、このあたりは好みの問題で、どちらの書き方でもいいのかなと思っています。
感覚としては、「各ループの最初に next_layer を空にして使い始める」か、「ループの最後に次のループのために空にしておく(後片付けしておく)」かという違いで、どちらの考え方もよくわかります。
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.
ありがとうございます。次の問題(https://github.com/MA-yo-TA/leetcode/pull/22)含めて色々書いてみた結果、ループの最後に後片付けするのが自分としてはしっくりくると感じることができました。