From f2d8eedba1e98a8da5c97093224cca35326e7cca Mon Sep 17 00:00:00 2001 From: kazukiii Date: Sun, 7 Jul 2024 02:13:02 -0700 Subject: [PATCH 1/2] =?UTF-8?q?step1,=20step2,=20step3=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arai60/maximum-depth-of-binary-tree/README.md | 27 +++++++++++++++++++ arai60/maximum-depth-of-binary-tree/step1.py | 11 ++++++++ arai60/maximum-depth-of-binary-tree/step2.py | 19 +++++++++++++ arai60/maximum-depth-of-binary-tree/step3.py | 11 ++++++++ 4 files changed, 68 insertions(+) create mode 100644 arai60/maximum-depth-of-binary-tree/README.md create mode 100644 arai60/maximum-depth-of-binary-tree/step1.py create mode 100644 arai60/maximum-depth-of-binary-tree/step2.py create mode 100644 arai60/maximum-depth-of-binary-tree/step3.py diff --git a/arai60/maximum-depth-of-binary-tree/README.md b/arai60/maximum-depth-of-binary-tree/README.md new file mode 100644 index 0000000..36e6dd3 --- /dev/null +++ b/arai60/maximum-depth-of-binary-tree/README.md @@ -0,0 +1,27 @@ +## 考察 +- 過去に解いたことあり +- 方針 + - DFS + - 左部分木と右部分木のdepthのmaxを取って1を追加すれば良さそう + - 帰りがけで計算したいので再帰で書きたいところ + - 再帰の深さが最大10^4になるのが気になるが、recursionlimitが550000に設定されているので大丈夫そう + - BFS + - 深さ(レベル)別に見ていけば、最後に探索するノードの深さが答え +- Step1ではDFSで実装してみる + +## Step1 +- 再帰DFSで実装 +- time: O(n), space: O(n) + +## Step2 +- BFSでも実装 + - キューのところをスタックにすれば非再帰DFSにもなる + - time: O(n), space: O(n) +- 他の人のPRを検索 + - 行きと帰りのラベルを付けてスタックに積めば、帰りがけの計算も非再帰で表現可能 + - https://discord.com/channels/1084280443945353267/1201211204547383386/1228400327800127648 + +## Step3 +- 1回目: 28s +- 2回目: 28s +- 3回目: 26s diff --git a/arai60/maximum-depth-of-binary-tree/step1.py b/arai60/maximum-depth-of-binary-tree/step1.py new file mode 100644 index 0000000..f6b7f66 --- /dev/null +++ b/arai60/maximum-depth-of-binary-tree/step1.py @@ -0,0 +1,11 @@ +# Definition for a binary tree node. +# 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 not root: + return 0 + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 diff --git a/arai60/maximum-depth-of-binary-tree/step2.py b/arai60/maximum-depth-of-binary-tree/step2.py new file mode 100644 index 0000000..4594194 --- /dev/null +++ b/arai60/maximum-depth-of-binary-tree/step2.py @@ -0,0 +1,19 @@ +# Definition for a binary tree node. +# 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: + nodes_to_visit = deque([(root, 1)]) + max_depth = 0 + + while nodes_to_visit: + node, depth = nodes_to_visit.popleft() + if not node: continue + max_depth = max(max_depth, depth) + nodes_to_visit.append((node.left, depth + 1)) + nodes_to_visit.append((node.right, depth + 1)) + + return max_depth diff --git a/arai60/maximum-depth-of-binary-tree/step3.py b/arai60/maximum-depth-of-binary-tree/step3.py new file mode 100644 index 0000000..f6b7f66 --- /dev/null +++ b/arai60/maximum-depth-of-binary-tree/step3.py @@ -0,0 +1,11 @@ +# Definition for a binary tree node. +# 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 not root: + return 0 + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 From 031e0bf237f4d1673254f54a3ec9df9b5a7b271f Mon Sep 17 00:00:00 2001 From: kazukiii Date: Mon, 8 Jul 2024 00:18:26 -0700 Subject: [PATCH 2/2] =?UTF-8?q?step4=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arai60/maximum-depth-of-binary-tree/README.md | 3 +++ arai60/maximum-depth-of-binary-tree/step4.py | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 arai60/maximum-depth-of-binary-tree/step4.py diff --git a/arai60/maximum-depth-of-binary-tree/README.md b/arai60/maximum-depth-of-binary-tree/README.md index 36e6dd3..894bab9 100644 --- a/arai60/maximum-depth-of-binary-tree/README.md +++ b/arai60/maximum-depth-of-binary-tree/README.md @@ -25,3 +25,6 @@ - 1回目: 28s - 2回目: 28s - 3回目: 26s + +## Step4 +- キューにdepthを入れないversionのBFSを追加 diff --git a/arai60/maximum-depth-of-binary-tree/step4.py b/arai60/maximum-depth-of-binary-tree/step4.py new file mode 100644 index 0000000..5dbfaab --- /dev/null +++ b/arai60/maximum-depth-of-binary-tree/step4.py @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# 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 not root: + return 0 + + max_depth = 0 + nodes_to_visit = deque([root]) + while nodes_to_visit: + max_depth += 1 + level_size = len(nodes_to_visit) + for _ in range(level_size): + node = nodes_to_visit.popleft() + if node.left: + nodes_to_visit.append(node.left) + if node.right: + nodes_to_visit.append(node.right) + + return max_depth