From 96e9011f6fe9a26f018289c5fa66736f77ea3253 Mon Sep 17 00:00:00 2001 From: shining-ai Date: Fri, 8 Mar 2024 14:20:14 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90Arai60=E3=80=9121=E5=95=8F?= =?UTF-8?q?=E7=9B=AE=20104=5FMaximum=20Depth=20of=20Binary=20Tree?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../level_1.py | 47 +++++++++++++++++ .../level_2.py | 50 +++++++++++++++++++ .../level_3.py | 12 +++++ 3 files changed, 109 insertions(+) create mode 100644 arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_1.py create mode 100644 arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_2.py create mode 100644 arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_3.py 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..3f3a132 --- /dev/null +++ b/arai60/21-29_Tree_BT_BST/21_104_Maximum Depth of Binary Tree/level_2.py @@ -0,0 +1,50 @@ +# 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_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) From 8da13b0013e57c3ba00cc2e31814faa630ad6922 Mon Sep 17 00:00:00 2001 From: shining-ai Date: Sun, 10 Mar 2024 08:23:38 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=81=AE=E5=86=85=E5=AE=B9=E3=82=92=E5=8F=8D=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../level_2.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) 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 index 3f3a132..d26b371 100644 --- 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 @@ -2,16 +2,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) + 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 @@ -21,14 +17,13 @@ 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) + max_depth = depth return max_depth