-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
- > 古い 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)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自分はnode.leftがNoneならqueueに追加しないかなと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
``` |
There was a problem hiding this comment.
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
こういう理由なので、BFS の書き方として、深さをペアとしていれるのも分かりやすいと思います。