From a91eddd77f3869afa6aeabd171baef2634b7e2cc Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Sat, 29 Jun 2024 12:57:41 +0900 Subject: [PATCH 1/3] phase1 --- arai60/minimum_depth_of_binary_tree/phase1.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 arai60/minimum_depth_of_binary_tree/phase1.py diff --git a/arai60/minimum_depth_of_binary_tree/phase1.py b/arai60/minimum_depth_of_binary_tree/phase1.py new file mode 100644 index 0000000..22efeb4 --- /dev/null +++ b/arai60/minimum_depth_of_binary_tree/phase1.py @@ -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: + queue.append((node.right, depth + 1)) + else: + queue.append((node.left, depth + 1)) + return minimum_depth From 42cd5f14ab74d5f0a278e3138a75628ed3b34b40 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Sat, 29 Jun 2024 12:57:51 +0900 Subject: [PATCH 2/3] phase2 --- arai60/minimum_depth_of_binary_tree/phase2.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 arai60/minimum_depth_of_binary_tree/phase2.py diff --git a/arai60/minimum_depth_of_binary_tree/phase2.py b/arai60/minimum_depth_of_binary_tree/phase2.py new file mode 100644 index 0000000..c75382e --- /dev/null +++ b/arai60/minimum_depth_of_binary_tree/phase2.py @@ -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 From 62217828821fab44e240186c9e859727840eee98 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Sat, 29 Jun 2024 12:58:03 +0900 Subject: [PATCH 3/3] phase3 --- arai60/minimum_depth_of_binary_tree/phase3.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 arai60/minimum_depth_of_binary_tree/phase3.py diff --git a/arai60/minimum_depth_of_binary_tree/phase3.py b/arai60/minimum_depth_of_binary_tree/phase3.py new file mode 100644 index 0000000..ca59933 --- /dev/null +++ b/arai60/minimum_depth_of_binary_tree/phase3.py @@ -0,0 +1,16 @@ +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.left and not node.right: + minimum_depth = depth + break + if node.left: + queue.append((node.left, depth + 1)) + if node.right: + queue.append((node.right, depth + 1)) + return minimum_depth