-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/111. Minimum Depth of Binary Tree #22
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,79 @@ | ||
## Step 1. Initial Solution | ||
|
||
- 葉が出てきたら終了できるBFSで実装するのが自然に感じた | ||
- rootがNoneの時の返り値は0じゃないといけないらしい | ||
- 初めはValueErrorにしていたが駄目だった | ||
|
||
```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 minDepth(self, root: Optional[TreeNode]) -> int: | ||
if root is None: | ||
return 0 | ||
remaining_nodes_and_depths: List[Tuple[TreeNode, int]] = deque([(root, 1)]) | ||
while remaining_nodes_and_depths: | ||
node, depth = remaining_nodes_and_depths.popleft() | ||
if node.left is None and node.right is None: | ||
return depth | ||
if node.left is not None: | ||
remaining_nodes_and_depths.append((node.left, depth + 1)) | ||
if node.right is not None: | ||
remaining_nodes_and_depths.append((node.right, depth + 1)) | ||
raise ValueError('Invalid nodes') | ||
``` | ||
|
||
### Complexity Analysis | ||
|
||
- 時間計算量:O(n) | ||
- 最悪の場合でも全てのノードを見る前に終わる | ||
- 空間計算量:O(n) | ||
|
||
## Step 2. Alternatives | ||
|
||
- min_depthを定義している人がいたがBFSなら不要という認識なので疑問に思った | ||
- depthをノードごとに保持しない方法もあるが、二重ループになって深くなるので微妙かなと感じた | ||
- ただ、こちらのやり方を好む人も多そう | ||
- 最後にエラー処理を入れているがあまり同じことをしてる人はいなさそうなので不要かもしれない | ||
- 木構造の高さの測り方として行きがけと帰りがけ・上から配ると下から集めるといった表現をしている人がいた | ||
- 高さを知りたいだけなら行きがけで良いのかなと感じた | ||
- あまり帰りがけのメリットが分からなかった | ||
- 再帰で書くと自然なのはこちらになりそう | ||
|
||
```python | ||
class Solution: | ||
def minDepth(self, root: Optional[TreeNode]) -> int: | ||
if root is None: | ||
return 0 | ||
if root.left is None and root.right is None: | ||
return 1 | ||
if root.left is None: | ||
return 1 + self.minDepth(root.right) | ||
if root.right is None: | ||
return 1 + self.minDepth(root.left) | ||
return 1 + min(self.minDepth(root.right), self.minDepth(root.left)) | ||
``` | ||
|
||
## Step 3. Final Solution | ||
|
||
- 3回のうち1回は二重ループで書いてみたが最後はほぼStep 1と同じなものがしっくりきた | ||
|
||
```python | ||
class Solution: | ||
def minDepth(self, root: Optional[TreeNode]) -> int: | ||
if root is None: | ||
return 0 | ||
remaining_nodes_and_depths: List[Tuple[TreeNode, int]] = 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. 好みだとは思いますが、個人的には 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. 右辺を見たら型はわかる、List ではない、の2点で書かなくても良いかなと思いました。型を deque[tuple[TreeNode, int]] のように書くのはNGなのでしょうか? 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. こちらの書き方でも問題ないようでした。ご指摘ありがとうございます。 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. ちなみにどういう型チェックを想定されていますか? |
||
while remaining_nodes_and_depths: | ||
node, depth = remaining_nodes_and_depths.popleft() | ||
if node is None: | ||
continue | ||
if node.left is None and node.right is None: | ||
return depth | ||
remaining_nodes_and_depths.append((node.left, depth + 1)) | ||
remaining_nodes_and_depths.append((node.right, depth + 1)) | ||
``` |
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.
高さを知りたいだけならば、行きでよいのは確かです。
もっとも、色々複雑なことをしないといけない場合はあるので、ある程度幅広く選択肢が見えた上で、どうしたいかを選択したほうがいいですね。
変わり種で、こういう書き方もありますよ。(帰りがけをループに直したもの。)
potrue/leetcode#22 (comment)