From 60054b7dbea9c242a7543b21ef0dcc14fe86d1dd Mon Sep 17 00:00:00 2001 From: shining-ai Date: Sun, 10 Mar 2024 14:45:58 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90Arai60=E3=80=9122=E5=95=8F?= =?UTF-8?q?=E7=9B=AE=20111=5FMinimum=20Depth=20of=20Binary=20Tree?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../level_1.py | 57 ++++++++++++++++++ .../level_2.py | 58 +++++++++++++++++++ .../level_3.py | 16 +++++ 3 files changed, 131 insertions(+) create mode 100644 arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_1.py create mode 100644 arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py create mode 100644 arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_3.py diff --git a/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_1.py b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_1.py new file mode 100644 index 0000000..0aeb833 --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_1.py @@ -0,0 +1,57 @@ +# BFS +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + node_queue = deque([(root, 0)]) + while node_queue: + node, depth = node_queue.popleft() + if not node.left and not node.right: + break + if node.left: + node_queue.append((node.left, depth + 1)) + if node.right: + node_queue.append((node.right, depth + 1)) + min_node_num = depth + 1 + return min_node_num + + +# DFS +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + def search_min_depth(node): + if not node.left and not node.right: + return 0 + left_depth = float(inf) + right_depth = float(inf) + if node.left: + left_depth = search_min_depth(node.left) + 1 + if node.right: + right_depth = search_min_depth(node.right) + 1 + min_depth = min(left_depth, right_depth) + return min_depth + + if not root: + return 0 + min_node_num = search_min_depth(root) + 1 + return min_node_num + + +# DFSをstackで実装 +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + node_depth_stack = [(root, 0)] + min_depth = float(inf) + while node_depth_stack: + node, depth = node_depth_stack.pop() + if not node.left and not node.right: + min_depth = min(min_depth, depth) + continue + if node.right: + node_depth_stack.append((node.right, depth + 1)) + if node.left: + node_depth_stack.append((node.left, depth + 1)) + min_node_num = min_depth + 1 + return min_node_num diff --git a/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py new file mode 100644 index 0000000..bd48045 --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py @@ -0,0 +1,58 @@ +# BFS +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + node_depth_queue = deque([(root, 0)]) + while node_depth_queue: + node, depth = node_depth_queue.popleft() + if not node.left and not node.right: + break + if node.left: + node_depth_queue.append((node.left, depth + 1)) + if node.right: + node_depth_queue.append((node.right, depth + 1)) + min_node_num = depth + 1 + return min_node_num + + +# DFS +# left_depthとright_depthをmin_depthに統合 +# min_depthの初期値をintの最大値に変更 +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + def search_min_depth(node): + if not node.left and not node.right: + return 0 + min_depth = sys.maxsize + if node.left: + min_depth = min(min_depth, search_min_depth(node.left) + 1) + if node.right: + min_depth = min(min_depth, search_min_depth(node.right) + 1) + return min_depth + + if not root: + return 0 + min_node_num = search_min_depth(root) + 1 + return min_node_num + + +# DFSをstackで実装 +# min_depthの初期値をintの最大値に変更 +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + node_depth_stack = [(root, 0)] + min_depth = sys.maxsize + while node_depth_stack: + node, depth = node_depth_stack.pop() + if not node.left and not node.right: + min_depth = min(min_depth, depth) + continue + if node.right: + node_depth_stack.append((node.right, depth + 1)) + if node.left: + node_depth_stack.append((node.left, depth + 1)) + min_node_num = min_depth + 1 + return min_node_num diff --git a/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_3.py b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_3.py new file mode 100644 index 0000000..1edddb1 --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_3.py @@ -0,0 +1,16 @@ + +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + node_depth_queue = deque([(root, 0)]) + while node_depth_queue: + node, depth = node_depth_queue.popleft() + if not node.left and not node.right: + break + if node.left: + node_depth_queue.append((node.left, depth + 1)) + if node.right: + node_depth_queue.append((node.right, depth + 1)) + min_node_num = depth + 1 + return min_node_num From 5732a9c4cc93451418301d37a28e40868a2d45c4 Mon Sep 17 00:00:00 2001 From: shining-ai Date: Mon, 11 Mar 2024 18:15:36 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=81=AE=E6=8C=87=E6=91=98=E4=BA=8B=E9=A0=85=E3=82=92=E5=8F=8D?= =?UTF-8?q?=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../level_2.py | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py index bd48045..ec436c8 100644 --- a/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py +++ b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py @@ -7,13 +7,11 @@ def minDepth(self, root: Optional[TreeNode]) -> int: while node_depth_queue: node, depth = node_depth_queue.popleft() if not node.left and not node.right: - break + return depth + 1 if node.left: node_depth_queue.append((node.left, depth + 1)) if node.right: node_depth_queue.append((node.right, depth + 1)) - min_node_num = depth + 1 - return min_node_num # DFS @@ -21,20 +19,16 @@ def minDepth(self, root: Optional[TreeNode]) -> int: # min_depthの初期値をintの最大値に変更 class Solution: def minDepth(self, root: Optional[TreeNode]) -> int: - def search_min_depth(node): - if not node.left and not node.right: - return 0 - min_depth = sys.maxsize - if node.left: - min_depth = min(min_depth, search_min_depth(node.left) + 1) - if node.right: - min_depth = min(min_depth, search_min_depth(node.right) + 1) - return min_depth - if not root: return 0 - min_node_num = search_min_depth(root) + 1 - return min_node_num + if not root.left and not root.right: + return 1 + min_depth = sys.maxsize + if root.left: + min_depth = min(min_depth, self.minDepth(root.left) + 1) + if root.right: + min_depth = min(min_depth, self.minDepth(root.right) + 1) + return min_depth # DFSをstackで実装 @@ -54,5 +48,4 @@ def minDepth(self, root: Optional[TreeNode]) -> int: node_depth_stack.append((node.right, depth + 1)) if node.left: node_depth_stack.append((node.left, depth + 1)) - min_node_num = min_depth + 1 - return min_node_num + return min_depth + 1