diff --git a/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_1.py b/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_1.py new file mode 100644 index 0000000..b20fee1 --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_1.py @@ -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 + 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) + 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 diff --git a/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_2.py b/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_2.py new file mode 100644 index 0000000..d26b371 --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_2.py @@ -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 diff --git a/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_3.py b/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_3.py new file mode 100644 index 0000000..8d321ea --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_3.py @@ -0,0 +1,12 @@ +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 + right_depth = search_max_depth(node.right) + 1 + max_depth = max(left_depth, right_depth) + return max_depth + + return search_max_depth(root)