-
Notifications
You must be signed in to change notification settings - Fork 0
Minimum depth of binary tree #36
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: arai60
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| # 幅優先で良い | ||
| minimum_depth = 0 | ||
| queue = deque([(root, 0)]) | ||
| while queue: | ||
| node, depth = queue.popleft() | ||
| if not node: | ||
| minimum_depth = depth | ||
| break | ||
| if node.right and node.left: | ||
| queue.append((node.right, depth + 1)) | ||
| queue.append((node.left, depth + 1)) | ||
| elif node.right is not None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (phase1のコードに突っ込み入れるのも野暮かもしれませんが) ここだけNoneを使って書いていて他は使ってないので違和感ありました。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。その通りです... |
||
| queue.append((node.right, depth + 1)) | ||
| else: | ||
| queue.append((node.left, depth + 1)) | ||
| return minimum_depth | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| """ | ||
| Reference: | ||
| fhiyo: https://github.com/fhiyo/leetcode/pull/24/files | ||
| rossy0213: https://github.com/rossy0213/leetcode/pull/11/files | ||
| hayashi-ay: https://github.com/hayashi-ay/leetcode/pull/26 | ||
| shining-ai: https://github.com/shining-ai/leetcode/pull/22/files | ||
| """ | ||
|
|
||
| # if文を簡潔に改善, 枝葉の判定をnode.right, node.leftどちらもnoneの時にすることに | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| # 幅優先で良い | ||
| minimum_depth = 0 | ||
| queue = deque([(root, 1)]) | ||
| while queue: | ||
| node, depth = queue.popleft() | ||
| if not node.right and not node.left: | ||
| # 枝葉 | ||
| minimum_depth = depth | ||
| break # ここで早期returnしてしまって, while文を抜けたらraise Exception("unreachable")する手もある(hayashi-ayさん) | ||
| if node.right: | ||
| queue.append((node.right, depth + 1)) | ||
| if node.left: | ||
| queue.append((node.left, depth + 1)) | ||
|
|
||
| return minimum_depth | ||
|
|
||
| # 再帰で深さ優先探索 | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| if not root.right: | ||
| return self.minDepth(root.left) + 1 | ||
| if not root.left: | ||
| return self.minDepth(root.right) + 1 | ||
| return min(self.minDepth(root.left), self.minDepth(root.right)) + 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| minimum_depth = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BFSで解くのであれば、leaf nodeを見つけた段階でdepthを返してしまって良いので、minimum_depthは不要かと思います。 |
||
| queue = deque([(root, 1)]) | ||
| while queue: | ||
| node, depth = queue.popleft() | ||
| if not node.left and not node.right: | ||
| minimum_depth = depth | ||
| break | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここでreturnしてしまって良いと思いました。 |
||
| if node.left: | ||
| queue.append((node.left, depth + 1)) | ||
| if node.right: | ||
| queue.append((node.right, depth + 1)) | ||
|
Comment on lines
+12
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None であっても気にせずに queue につっこんで、出てきたやつを continue するという手もあります。 |
||
| return minimum_depth | ||
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.
phase2, 3だと変えてるので違和感あったのかなと思いますが、(node, nodeの一つ上の深さ) の組をキューに入れているのは理解するの難しいなと思いました。
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.
そうですね, phase1でわかりにくい+書くなら少しコメントを書く必要があると感じたのでphase2とphase3で変更しました