From e156ff1b0e706101f08017ac45a2a089790c03a9 Mon Sep 17 00:00:00 2001 From: NobukiFukui Date: Sun, 7 Jul 2024 00:08:50 +0900 Subject: [PATCH 1/2] Q2-10-1st_3rd --- md/2-10_MaximumDepthofBinaryTree.md | 67 +++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 md/2-10_MaximumDepthofBinaryTree.md diff --git a/md/2-10_MaximumDepthofBinaryTree.md b/md/2-10_MaximumDepthofBinaryTree.md new file mode 100644 index 0000000..c59cb80 --- /dev/null +++ b/md/2-10_MaximumDepthofBinaryTree.md @@ -0,0 +1,67 @@ +# 2-10_MaximumDepthofBinaryTree +Author: WaveAlchemist +Given the root of a binary tree, return its maximum depth. + +A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. + +# 1st-2nd +Diameter of Binary Treeで実装した再帰を思い出して解答したが +return 0のところを1としてしまい不正解になってしまった +後でDiameter of Binary Treeを見て修正した + +``` Python +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + left_depth = self.maxDepth(root.left) + right_depth = self.maxDepth(root.right) + depth = max(left_depth, right_depth) + 1 + return depth +``` +# 3rd +discord上のほかの人が解答されたコードを見て自分で再構築 +- https://github.com/fhiyo/leetcode/pull/23/files + - 幅優先探索  BFS + - 深さ優先探索 DFS +- https://discordapp.com/channels/1084280443945353267/1227073733844406343/1236695050902048899 + - + +``` Python +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + def append_node(nodes, node): + if not node: + return + nodes.append(node) + if not root: + return 0 + depth = 0 + nodes = [root] + while nodes: + next_depth_nodes = [] + for node in nodes: + append_node(next_depth_nodes, node.left) + append_node(next_depth_nodes, node.right) + nodes = next_depth_nodes + depth += 1 + return depth +``` + +``` Python +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + max_depth = 0 + nodes_with_depth = [(root, 1)] + while nodes_with_depth: + node, depth = nodes_with_depth.pop() + max_depth = max(depth, max_depth) + if node.left: + nodes_with_depth.append((node.left, depth + 1)) + if node.right: + nodes_with_depth.append((node.right, depth + 1)) + return max_depth +``` + From 3c3d8fc645f223a1ead63c5d9955859798c80aa1 Mon Sep 17 00:00:00 2001 From: NobukiFukui Date: Sun, 28 Jul 2024 14:06:54 +0900 Subject: [PATCH 2/2] Q2-10-4th --- md/2-10_MaximumDepthofBinaryTree.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/md/2-10_MaximumDepthofBinaryTree.md b/md/2-10_MaximumDepthofBinaryTree.md index c59cb80..b44c059 100644 --- a/md/2-10_MaximumDepthofBinaryTree.md +++ b/md/2-10_MaximumDepthofBinaryTree.md @@ -65,3 +65,23 @@ class Solution: return max_depth ``` +# 4th +odaさんのコメントをもとに変更 +pop してから、not node を確認する手もあるでしょう。その場合は、先頭の if not root もいらないかもしれません。 + +``` Python +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + max_depth = 0 + nodes_with_depth = [(root, 1)] + while nodes_with_depth: + node, depth = nodes_with_depth.pop() + if not node: + return 0 + max_depth = max(depth, max_depth) + if node.left: + nodes_with_depth.append((node.left, depth + 1)) + if node.right: + nodes_with_depth.append((node.right, depth + 1)) + return max_depth +``` \ No newline at end of file