Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions 0111-minimum-depth-of-binary-tree/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

maximum depth of binary treeを参考に、左や右のnodeがない場合の処理だけ書いたら解けた。

### step2

変更点なし。

### step3

3回通すまで書き直し。
21 changes: 21 additions & 0 deletions 0111-minimum-depth-of-binary-tree/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}

int leftDepth = minDepth(root->left);
int rightDepth = minDepth(root->right);

if (leftDepth == 0) {
return rightDepth + 1;
}

if (rightDepth == 0) {
return leftDepth + 1;
}

return min(leftDepth, rightDepth) + 1;
}
};
21 changes: 21 additions & 0 deletions 0111-minimum-depth-of-binary-tree/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}

int leftDepth = minDepth(root->left);
int rightDepth = minDepth(root->right);

if (leftDepth == 0) {
return rightDepth + 1;
}

if (rightDepth == 0) {
return leftDepth + 1;
}

return min(leftDepth, rightDepth) + 1;
}
};
21 changes: 21 additions & 0 deletions 0111-minimum-depth-of-binary-tree/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}

int leftDepth = minDepth(root->left);
int rightDepth = minDepth(root->right);

if (leftDepth == 0) {
return rightDepth + 1;
}

if (rightDepth == 0) {
return leftDepth + 1;
}

return min(leftDepth, rightDepth) + 1;
}
};