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..ec436c8 --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/22_111_Minimum Depth of Binary Tree/level_2.py @@ -0,0 +1,51 @@ +# 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: + 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)) + + +# DFS +# left_depthとright_depthをmin_depthに統合 +# min_depthの初期値をintの最大値に変更 +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + 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で実装 +# 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)) + return min_depth + 1 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