-
Notifications
You must be signed in to change notification settings - Fork 0
Q2-10 Maximum Depth of Binary Tree #38
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?
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,87 @@ | ||
| # 2-10_MaximumDepthofBinaryTree | ||
| Author: WaveAlchemist | ||
| Given the root of a binary tree, return its maximum depth. | ||
|
|
||
| A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. | ||
|
|
||
| # 1st-2nd | ||
| Diameter of Binary Treeで実装した再帰を思い出して解答したが | ||
| return 0のところを1としてしまい不正解になってしまった | ||
| 後でDiameter of Binary Treeを見て修正した | ||
|
|
||
| ``` Python | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| left_depth = self.maxDepth(root.left) | ||
| right_depth = self.maxDepth(root.right) | ||
| depth = max(left_depth, right_depth) + 1 | ||
| return depth | ||
| ``` | ||
| # 3rd | ||
| discord上のほかの人が解答されたコードを見て自分で再構築 | ||
| - https://github.com/fhiyo/leetcode/pull/23/files | ||
| - 幅優先探索 BFS | ||
| - 深さ優先探索 DFS | ||
| - https://discordapp.com/channels/1084280443945353267/1227073733844406343/1236695050902048899 | ||
| - | ||
|
|
||
| ``` Python | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| def append_node(nodes, node): | ||
| if not node: | ||
| return | ||
| nodes.append(node) | ||
|
Comment on lines
+33
to
+36
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. 関数内関数でも切れ目 (36行目と37行目の間) は1行空ける方が多分主流だと思います。関数はそれがひとつのまとまりであることがパッと見て分かる方が嬉しいので。
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. ありがとうございます.ついついやってしまうので,気を付けます |
||
| if not root: | ||
| return 0 | ||
| depth = 0 | ||
| nodes = [root] | ||
| while nodes: | ||
| next_depth_nodes = [] | ||
| for node in nodes: | ||
| append_node(next_depth_nodes, node.left) | ||
| append_node(next_depth_nodes, node.right) | ||
| nodes = next_depth_nodes | ||
| depth += 1 | ||
| return depth | ||
| ``` | ||
|
|
||
| ``` Python | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| max_depth = 0 | ||
| nodes_with_depth = [(root, 1)] | ||
| while nodes_with_depth: | ||
| node, depth = nodes_with_depth.pop() | ||
|
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. pop してから、not node を確認する手もあるでしょう。その場合は、先頭の if not root もいらないかもしれません。
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. node, depth = nodes_with_depth.pop()の下に を追加で行けますね. |
||
| max_depth = max(depth, max_depth) | ||
| if node.left: | ||
| nodes_with_depth.append((node.left, depth + 1)) | ||
| if node.right: | ||
| nodes_with_depth.append((node.right, depth + 1)) | ||
| return max_depth | ||
| ``` | ||
|
|
||
| # 4th | ||
| odaさんのコメントをもとに変更 | ||
| pop してから、not node を確認する手もあるでしょう。その場合は、先頭の if not root もいらないかもしれません。 | ||
|
|
||
| ``` Python | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| max_depth = 0 | ||
| nodes_with_depth = [(root, 1)] | ||
| while nodes_with_depth: | ||
| node, depth = nodes_with_depth.pop() | ||
| if not node: | ||
| return 0 | ||
| max_depth = max(depth, max_depth) | ||
| if node.left: | ||
| nodes_with_depth.append((node.left, depth + 1)) | ||
| if node.right: | ||
| nodes_with_depth.append((node.right, depth + 1)) | ||
| return max_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.
None かどうかを調べるとき、
if node is None:と明示的に書くことを推奨するスタイルガイドがあります。https://google.github.io/styleguide/pyguide.html#2144-decision