Skip to content

Maximum Depth of Binary Tree.md #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions 104. Maximum Depth of Binary Tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
URL: https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

# Step 1

- 実装時間: 5分
- ノード数をnとして
- 時間計算量: O(n)
- 空間計算量: O(n)

```python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
nodes_and_depths = [(root, 1)]
while nodes_and_depths:
node, depth = nodes_and_depths.pop()
if node is None:
continue
max_depth = max(max_depth, depth)
nodes_and_depths.append((node.left, depth + 1))
nodes_and_depths.append((node.right, depth + 1))
return max_depth
```

leetcodeの活動を通して、ループへの慣れが出てきた。

# Step 2

- 参考にしたURL
- https://github.com/colorbox/leetcode/pull/35
- https://github.com/tarinaihitori/leetcode/pull/21
- https://github.com/haniwachann/leetcode/pull/4
- https://github.com/rihib/leetcode/pull/41
- https://github.com/hroc135/leetcode/pull/20
- https://github.com/seal-azarashi/leetcode/pull/20
- https://github.com/goto-untrapped/Arai60/pull/45
- https://github.com/Ryotaro25/leetcode_first60/pull/23
- https://github.com/kazukiii/leetcode/pull/22
- https://github.com/NobukiFukui/Grind75-ProgrammingTraining/pull/38
- https://github.com/Yoshiki-Iwasa/Arai60/pull/23
- https://github.com/SuperHotDogCat/coding-interview/pull/34
- https://github.com/fhiyo/leetcode/pull/23
- https://github.com/nittoco/leetcode/pull/14

- バリエーション
- 上から/下から
- stackじゃなくてqueueにすると、maxでの比較が不要になる。
- ループ/再帰関数
- stackにdepthも入れる/階層ごとに二重ループを回す
- > 階層ごとにしたいときには、私は List 2つで作るのが好みです。
- https://github.com/tarinaihitori/leetcode/pull/21/files#r1859073373
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://discord.com/channels/1084280443945353267/1201211204547383386/1219179255615717399

一つの変数に、2つの違う種類のものを入れておいて、その境界を個数で管理している
データの整合性が取れているということは、読んでいる人からすると全部読み終わらないと分からない

こういう理由なので、BFS の書き方として、深さをペアとしていれるのも分かりやすいと思います。


- > 古い C の流儀で、変数は上の方にまとめて宣言というのはありますが、現代的ではないかと思います。
- https://github.com/haniwachann/leetcode/pull/4/files#r1845319724a

- > 私は、比較的、stack や queue という名前は状況次第で許容側です。
- https://github.com/rihib/leetcode/pull/41/files#r1782885611
- `nodes_and_depths`という変数名も`node_and_depth_stack`にすると使われ方も示せてよさそう

```python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
node_and_depth_queue = deque([(root, 1)])
while node_and_depth_queue:
node, depth = node_and_depth_queue.popleft()
if node is None:
continue
max_depth = depth
node_and_depth_queue.append((node.left, depth + 1))
node_and_depth_queue.append((node.right, depth + 1))
return max_depth
```

# Step 3

- ノード数をnとして
- 時間計算量: O(n)
- 空間計算量: O(n)

```python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
node_and_depth_queue = deque([(root, 1)])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queueを使うかlistを2つ使うかも好みですかね。自分はlistを2つ使う派です。
こんな感じ。

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        nodes = [root]
        depth = 0
        while nodes:
            depth += 1
            next_nodes = []
            for node in nodes:
                if node.left:
                    next_nodes.append(node.left)
                if node.right:
                    next_nodes.append(node.right)
            nodes = next_nodes
        return depth

while node_and_depth_queue:
node, depth = node_and_depth_queue.popleft()
if node is None:
continue
max_depth = depth
node_and_depth_queue.append((node.left, depth + 1))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分はnode.leftがNoneならqueueに追加しないかなと思います。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは、入る前にチェックと出る前にチェックがあって、入る前にチェックするならば、はじめに root もチェックしないといけませんね。

入る前にチェックするならばチェックが3箇所に散るので関数にするのも一つです。

node_and_depth_queue.append((node.right, depth + 1))
return max_depth
```