-
Notifications
You must be signed in to change notification settings - Fork 0
【Arai60】21問目 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
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,47 @@ | ||||||
| # DFS | ||||||
| class Solution: | ||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||
|
|
||||||
| def search_max_depth(node): | ||||||
| if not node: | ||||||
| return 0 | ||||||
| left_depth = search_max_depth(node.left) + 1 | ||||||
|
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. left_depthという命名だと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. 深さだとrootが0カウントになってしまうということですね。 |
||||||
| right_depth = search_max_depth(node.right) + 1 | ||||||
| max_depth = max(left_depth, right_depth) | ||||||
| return max_depth | ||||||
|
|
||||||
| return search_max_depth(root) | ||||||
|
|
||||||
|
|
||||||
| # BFS | ||||||
| class Solution: | ||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||
| if not root: | ||||||
| return 0 | ||||||
| node_depth_queue = deque([(root, 1)]) | ||||||
| max_depth = 0 | ||||||
| while node_depth_queue: | ||||||
| node, depth = node_depth_queue.popleft() | ||||||
| if node.left: | ||||||
| node_depth_queue.append((node.left, depth + 1)) | ||||||
| if node.right: | ||||||
| node_depth_queue.append((node.right, depth + 1)) | ||||||
| max_depth = max(max_depth, depth) | ||||||
|
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なのでmax取らなくても良いですね。
Suggested change
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. 一番最後のdepthが最大値で決まってますね。 |
||||||
| return max_depth | ||||||
|
|
||||||
|
|
||||||
| # stackで深さ優先探索 | ||||||
| class Solution: | ||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||
| if not root: | ||||||
| return 0 | ||||||
| stack_node_depth = [(root, 1)] | ||||||
| max_depth = 1 | ||||||
| while stack_node_depth: | ||||||
| node, depth = stack_node_depth.pop() | ||||||
| if node.left: | ||||||
|
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. 好みの問題ですが、DFSの探索をするときは左側から見ていくのが自然な気がします。そうなるとstackに積む順番はnode.right, node.leftの順になります。
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. 確かに探索は左からが自然だと思いました。 |
||||||
| stack_node_depth.append((node.left, depth + 1)) | ||||||
| if node.right: | ||||||
| stack_node_depth.append((node.right, depth + 1)) | ||||||
| max_depth = max(max_depth, depth) | ||||||
| return max_depth | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # DFS | ||
| # 特に変更なし | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| left_depth = self.maxDepth(root.left) + 1 | ||
| right_depth = self.maxDepth(root.right) + 1 | ||
| max_depth = max(left_depth, right_depth) | ||
| return max_depth | ||
|
|
||
|
|
||
| # BFS | ||
| # 特に変更なし | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| node_depth_queue = deque([(root, 1)]) | ||
| while node_depth_queue: | ||
| node, depth = node_depth_queue.popleft() | ||
| if node.left: | ||
| node_depth_queue.append((node.left, depth + 1)) | ||
| if node.right: | ||
| node_depth_queue.append((node.right, depth + 1)) | ||
| max_depth = depth | ||
| return max_depth | ||
|
|
||
|
|
||
| # stackで深さ優先探索 | ||
| # 特に変更なし | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| stack_node_depth = [(root, 1)] | ||
| max_depth = 1 | ||
| while stack_node_depth: | ||
| node, depth = stack_node_depth.pop() | ||
| if node.left: | ||
| stack_node_depth.append((node.left, depth + 1)) | ||
| if node.right: | ||
| stack_node_depth.append((node.right, depth + 1)) | ||
| max_depth = max(max_depth, depth) | ||
| return max_depth |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
|
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. 好みの問題かもですが、ヘルパー関数作らなくても書けますね。 |
||
|
|
||
| def search_max_depth(node): | ||
| if not node: | ||
| return 0 | ||
| left_depth = search_max_depth(node.left) + 1 | ||
| right_depth = search_max_depth(node.right) + 1 | ||
| max_depth = max(left_depth, right_depth) | ||
| return max_depth | ||
|
|
||
| return search_max_depth(root) | ||
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.
補助関数に分けても良いと思いますし、 maxDepth() と引数が同じため、 maxDepth() を直接再帰呼び出ししても良いと思いました。
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がない時の戻り値がすぐ分かって、少し分かりやすくなると思いました。