Skip to content
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
171 changes: 171 additions & 0 deletions 111-minimum-depth-of-binary-tree/111-minimum-depth-of-binary-tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Minimum Depth of Binary Tree
* 問題: https://leetcode.com/problems/minimum-depth-of-binary-tree/
* 言語: C++

## Step1

- 前回と同じように、DFSとBFSの両方で解いてみた。
- DFSは左ノードや右ノードがない場合のケースも考慮する必要がある。

```cpp
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) {
return 0;
}
if (!root->left) {
return minDepth(root->right) + 1;
}
if (!root->right) {
return minDepth(root->left) + 1;
}

return min(minDepth(root->left), minDepth(root->right)) + 1;
}
};
```

- BFSはqueueでノードと深さを管理する。葉ノードに達したときの深さが最小の深さなのでreturn
- ノードと深さを別にする解き方もあるが、ループが増えるので一緒にqueueで管理した方が楽だと思ったので、一緒にして実装。

```cpp
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) {
return 0;
}
queue<pair<TreeNode*, int>> node_and_depth;
node_and_depth.emplace(root, 1);
while (!node_and_depth.empty()) {
auto [node, depth] = node_and_depth.front();
node_and_depth.pop();
if (!node->left && !node->right) {
return depth;
}
if (node->left) {
node_and_depth.emplace(node->left, depth + 1);
}
if (node->right) {
node_and_depth.emplace(node->right, depth + 1);
}
}
return 0;
}
};
```

## Step2

- https://github.com/irohafternoon/LeetCode/pull/24/files
- https://github.com/nktr-cp/leetcode/pull/23/files
- 深さごとに管理する方法で解いてみた。
- https://github.com/irohafternoon/LeetCode/pull/24/files#r2052736369
- とりあえずpush_backして、nullptrだったら飛ばすのでもいいのか。
- 無限ループにしたら、末行でreturnしなくて済むのか。
- https://stackoverflow.com/questions/58520107/do-i-need-a-return-statement-after-an-infinite-loop?utm_source=chatgpt.com
- 問題はないそうだが、コンパイラによっては警告を出すらしい。return文を省略しなくてもいい気がする。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

実行されることがないコードを Dead code といいます。むしろ書かないほうがいいと私は思いますね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

デッドコードと言うんですね。
デッドコードがあると読みやすさや保守性が低下するため、書かない方がよい、ということでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そういうことです。
誤った引数によっては到達する場合 never reach などと書くこともありますが。
https://source.chromium.org/search?q=%22never%20reach%22


```cpp
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) {
return 0;
}
vector<TreeNode*> current_nodes{root};
int depth = 1;
while (true) {
vector<TreeNode*> next_nodes;
for (auto node : current_nodes) {
if (!node->left && !node->right) {
return depth;
}
if (node->left) {
next_nodes.push_back(node->left);
}
if (node->right) {
next_nodes.push_back(node->right);
}
}
current_nodes.swap(next_nodes);
++depth;
}
}
};
```

- とりあえずキューに入れて、nullptrだったら飛ばす
- root == nullptrだったら、末行で0が返ってくる
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ぱっと見でちょっと分からなかったので個人的には最初にチェックしてreturnしてしまうほうが分かりやすいなと思いました

- こっちの方がすっきりしていて好み。

```cpp
class Solution {
public:
int minDepth(TreeNode* root) {
queue<pair<TreeNode*, int>> node_and_depth;
node_and_depth.emplace(root, 1);
while (!node_and_depth.empty()) {
auto [node, depth] = node_and_depth.front();
node_and_depth.pop();
if (!node) {
continue;
}
if (!node->left && !node->right) {
return depth;
}
node_and_depth.emplace(node->left, depth + 1);
node_and_depth.emplace(node->right, depth + 1);
}
return 0;
}
};
```

## Step3

- DFS

```cpp
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) {
return 0;
}
if (!root->left) {
return minDepth(root->right) + 1;
}
if (!root->right) {
return minDepth(root->left) + 1;
}
return min(minDepth(root->left), minDepth(root->right)) + 1;
}
};
```

- BFS

```cpp
class Solution {
public:
int minDepth(TreeNode* root) {
queue<pair<TreeNode*, int>> node_and_depth;
node_and_depth.emplace(root, 1);
while (!node_and_depth.empty()) {
auto [node, depth] = node_and_depth.front();
node_and_depth.pop();
if (!node) {
continue;
}
if (!node->left && !node->right) {
return depth;
}
node_and_depth.emplace(node->left, depth + 1);
node_and_depth.emplace(node->right, depth + 1);
}
return 0;
}
};
Comment on lines +152 to +170
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読みやすいです

```