diff --git a/104-Maximum-Depth-of-Binary-Tree/note.md b/104-Maximum-Depth-of-Binary-Tree/note.md new file mode 100644 index 0000000..66adbb4 --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/note.md @@ -0,0 +1,26 @@ +# 104. Maximum Depth of Binary Tree + +https://leetcode.com/problems/maximum-depth-of-binary-tree/ + +## step1(まず通す) + +深さ優先探索だと一番深いところにいつ当たるかわからないので、max_depth を持っておいて更新していく。 + +幅優先探索だと木なら同じ高さのノードを順番に見ていく形になるので最後に見たノードが一番深い。 + +ということで幅優先探索する。いつも (node, distance) のキューでやってたが、今回は階層ごとに見ていって一番深いところの深さを得るというのを素朴に表現してみる。 + +## step2(整形&他の人のコードを読む) + +- https://github.com/hayashi-ay/leetcode/pull/22/changes + - Python では再帰が好まれない印象なので避けたが、木のような構造が再帰的なやつは再帰で書くととてもわかりやすい + - [浅井健一『プログラミングの基礎』(サイエンス社)](https://www.saiensu.co.jp/search/?isbn=978-4-7819-1160-1&y=2007#detail)(ちなみにこんな名前で OCaml。名著。)ではデータ構造の再帰的な定義と一対一で対応した再帰を「構造にしたがった再帰」と呼んでいるが、今回のようなものはまさにそう。 + - 木の再帰的な定義: + - None は木である + - ある一つのノードがあってその子が全て木であるものは木である。 + - この問題を解く再帰関数: + - None なら高さは 0 である + - ある一つのノードにおける高さはその子の高さの max に 1 を加えたものである。 + - 今回は ノード数 <= 10 ^ 4 なので、再帰の深さは log 10 ^ 4 ~ 13 とかなので Python のリミット的にもスタックのサイズ的にも基本的に問題ない。 + +## step3(10分以内にさっとかける \* 3回) diff --git a/104-Maximum-Depth-of-Binary-Tree/step1.py b/104-Maximum-Depth-of-Binary-Tree/step1.py new file mode 100644 index 0000000..26d0402 --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/step1.py @@ -0,0 +1,29 @@ +from typing import Optional + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + + nodes = [root] + next_layer = [] + depth = 0 + while nodes: + depth += 1 + for node in nodes: + if node.left is not None: + next_layer.append(node.left) + if node.right is not None: + next_layer.append(node.right) + nodes = next_layer + next_layer = [] + + return depth diff --git a/104-Maximum-Depth-of-Binary-Tree/step2.py b/104-Maximum-Depth-of-Binary-Tree/step2.py new file mode 100644 index 0000000..81eebcb --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/step2.py @@ -0,0 +1,15 @@ +from typing import Optional + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 diff --git a/104-Maximum-Depth-of-Binary-Tree/step3.py b/104-Maximum-Depth-of-Binary-Tree/step3.py new file mode 100644 index 0000000..26d0402 --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/step3.py @@ -0,0 +1,29 @@ +from typing import Optional + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + + nodes = [root] + next_layer = [] + depth = 0 + while nodes: + depth += 1 + for node in nodes: + if node.left is not None: + next_layer.append(node.left) + if node.right is not None: + next_layer.append(node.right) + nodes = next_layer + next_layer = [] + + return depth