diff --git a/README.md b/README.md index b9c1787..a11001a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LeetCode -## 問題: +## 問題: [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) ## 前提 diff --git a/step1.md b/step1.md index 54ebbc1..14eb34d 100644 --- a/step1.md +++ b/step1.md @@ -1,7 +1,76 @@ # Step 1 +## Recursive DFS + +- base case: nodeがNoneのとき0を返す +- recursive step: 左右のうちより深い方に1を足して返す + +```python +# 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 1 + max(self.maxDepth(root.right), self.maxDepth(root.left)) +``` + +時間計算量: O(n) + +空間計算量: O(n) + +## Iterative DFS + ```python +# 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: + stack = [[root, 1]] + max_depth = 0 + while stack: + node, depth = stack.pop() + if node: + max_depth = max(max_depth, depth) + stack.append([node.left, depth + 1]) + stack.append([node.right, depth + 1]) + return max_depth ``` -時間計算量: -空間計算量: +時間計算量: O(n) + +空間計算量: O(n) + +## BFS + +```python +# 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: + queue = deque([[root, 1]]) + res = 0 + while queue: + node, depth = queue.popleft() + if node: + res = depth + queue.append([node.left, depth + 1]) + queue.append([node.right, depth + 1]) + return res +``` + +時間計算量: O(n) + +空間計算量: O(n) diff --git a/step2.md b/step2.md index 6ec3360..bdf5af3 100644 --- a/step2.md +++ b/step2.md @@ -1,4 +1,21 @@ # Step 2 ```python +# 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: + queue = deque([[root, 1]]) + res = 0 + while queue: + node, depth = queue.popleft() + if node: + res = depth + queue.append([node.left, depth + 1]) + queue.append([node.right, depth + 1]) + return res ``` diff --git a/step3.md b/step3.md index 522daa6..0e4966a 100644 --- a/step3.md +++ b/step3.md @@ -1,9 +1,29 @@ # Step 3 +- dequeを書き慣れていないのでBFSでStep 3を行う + ```python +# 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: + queue = deque([[root, 1]]) + res = 0 + while queue: + node, depth = queue.popleft() + if node: + res = depth + queue.append([node.left, depth + 1]) + queue.append([node.right, depth + 1]) + return res ``` -1回目: 分 秒 -2回目: 分 秒 +1回目: 1分 30秒 + +2回目: 1分 33秒 -3回目: 分 秒 +3回目: 1分 32秒