diff --git a/problems/104.maximum-depth-of-binary-tree/memo.md b/problems/104.maximum-depth-of-binary-tree/memo.md new file mode 100644 index 0000000..9860724 --- /dev/null +++ b/problems/104.maximum-depth-of-binary-tree/memo.md @@ -0,0 +1,52 @@ +## step1 +- 幅優先探索か深さ優先探索で解けそう +- 幅優先探索の方が描き慣れてるので幅優先で行く +- Nodeが0の場合もある +- 最大でも10^4なので計算量O(n)で問題なし +- 5分ほどで書けた + - rootがNoneの場合でWAを出してしまった + - 気をつけてはいたが `while [null]:`がTrueになってwhileの中が実行された + - `if root is None: return 0`と最初に処理するようにしたがもっと良い感じに書く方法があれば書きたい + +## step2 +- 他の人のコードを見る +- https://github.com/mamo3gr/arai60/pull/21 + - 再帰に慣れていないので理解に時間がかかった + - 意外とコメントにあった`return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1`の方が1行で複雑そうなのに理解しやすく感じた + - `to_visit`は幅広く使えて良い命名だと思った + - コメントにあった下記はかっこいいけど難しく感じる +```py +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + + return max(self.maxDepth(child) for child in (root.left, root.right)) + 1 +``` +- https://github.com/Yuto729/LeetCode_arai60/pull/26/files + - tupleにしてdepthを一緒に更新していくのもありだと思ったし選択肢になかった + - `こちらのコードでは先にnullチェックを行っていますが,とりあえずスタックに追加してからnullチェックをする手もあります:`これも選択肢になかった + - ほぼ同じコードを書いたが`[root] の中身があることによって、root の depth が1だと分かるわけですね。`というコメントもあるので参考にする。 + +- https://github.com/plushn/SWE-Arai60/pull/21/files + - `pop直後にやる方法について、prosはNoneチェックを現状のコードで3箇所あるのを1箇所に集約できる点、consは一時的にNoneがキューに入ることでループが多く回る点です。BFSにて幾何級数的に増加することがあるそうです。` + - `if node.left: はNoneを弾く意図だと思いますが、注意深い読み手はspecial method TreeNode.__bool__()がオーバーライドされてないか気になってしまいます。また実際にTreeNode.__bool__()の実装次第ではnode.leftがNoneでなくとも意図せずimplicit falsyが成立しうる点が危ういと感じるので、個人的にはif node.left is not Noneの方が好ましいと感じます。` + +- step2は苦手意識がある再帰でやってみる + - 書いた結果、書く分量が劇的に減るので書く際の時間や読んで理解するまでの時間も考えこちらの方が良い気がしてきた + - ただどのような順番でstackに積まれていくかをイメージするのが苦手なのでstackの数が無闇に増えていないことを確認するのに時間がかかった + - 今回は木の深さがmax(10程度なはず)なのでpythonのデフォルトの1000以下で問題なし + A=>B=>D=>E=>C +``` + A + / \ + B C + / \ + D E + +``` + +## step3 +- step3はdepthも一緒に更新していく方法にした +- どの方法も5分以内に書けた + diff --git a/problems/104.maximum-depth-of-binary-tree/step1.py b/problems/104.maximum-depth-of-binary-tree/step1.py new file mode 100644 index 0000000..054f689 --- /dev/null +++ b/problems/104.maximum-depth-of-binary-tree/step1.py @@ -0,0 +1,34 @@ +# +# @lc app=leetcode id=104 lang=python3 +# +# [104] Maximum Depth of Binary Tree +# + +# @lc code=start +# Definition for a binary tree node. +# 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: + depth = 0 + if root is None: + return 0 + parents = [root] + while parents: + children = [] + for parent in parents: + if parent.left: + children.append(parent.left) + if parent.right: + children.append(parent.right) + + parents = children + depth += 1 + + return depth + + +# @lc code=end diff --git a/problems/104.maximum-depth-of-binary-tree/step2.py b/problems/104.maximum-depth-of-binary-tree/step2.py new file mode 100644 index 0000000..1416e7a --- /dev/null +++ b/problems/104.maximum-depth-of-binary-tree/step2.py @@ -0,0 +1,21 @@ +# +# @lc app=leetcode id=104 lang=python3 +# +# [104] Maximum Depth of Binary Tree +# + +# @lc code=start +# Definition for a binary tree node. +# 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 + + +# @lc code=end diff --git a/problems/104.maximum-depth-of-binary-tree/step3.py b/problems/104.maximum-depth-of-binary-tree/step3.py new file mode 100644 index 0000000..7fab336 --- /dev/null +++ b/problems/104.maximum-depth-of-binary-tree/step3.py @@ -0,0 +1,33 @@ +# +# @lc app=leetcode id=104 lang=python3 +# +# [104] Maximum Depth of Binary Tree +# + +# @lc code=start +# Definition for a binary tree node. +# 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 + + to_visit = [(root, 1)] + max_depth = 0 + + while to_visit: + node, depth = to_visit.pop() + max_depth = max(depth, max_depth) + if node.left is not None: + to_visit.append((node.left, depth + 1)) + if node.right is not None: + to_visit.append((node.right, depth + 1)) + + return max_depth + + +# @lc code=end