-
Notifications
You must be signed in to change notification settings - Fork 0
111. Minimum Depth of Binary Tree #36
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Solve Time : 07:17 | ||
Time : O(V + E) | ||
Space : O(V) | ||
root ~ leafまでの距離、という部分を読み違えて時間を多少ロスしたものの、特につまるところなく解けた。 | ||
*/ | ||
class Solution { | ||
public: | ||
int minDepth(TreeNode* root) { | ||
queue<pair<TreeNode*, int>> nodes_and_depths; | ||
nodes_and_depths.emplace(root, 1); | ||
while (!nodes_and_depths.empty()) { | ||
auto [node, depth] = nodes_and_depths.front(); | ||
nodes_and_depths.pop(); | ||
if (!node) { | ||
continue; | ||
} | ||
if (node->left == nullptr && node->right == nullptr) { | ||
return depth; | ||
} | ||
nodes_and_depths.emplace(node->left, depth + 1); | ||
nodes_and_depths.emplace(node->right, depth + 1); | ||
} | ||
return 0; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Time : O(V + E) | ||
Space : O(V) | ||
|
||
再帰,DFSで解いてみる。 | ||
rootの処理をどうしても分岐で処理できなかったので最初のrootの面倒を見る関数と再帰処理をする関数とに分けた。 | ||
*/ | ||
class Solution { | ||
public: | ||
int minDepth(TreeNode* root) { | ||
if (!root) { | ||
return 0; | ||
} | ||
return minDepthRecursive(root); | ||
} | ||
private: | ||
int minDepthRecursive(TreeNode* root) { | ||
if (!root) { | ||
return numeric_limits<int>::max(); | ||
} | ||
if (root->left == nullptr && root->right == nullptr) { | ||
return 1; | ||
} | ||
return min(minDepthRecursive(root->left), minDepthRecursive(root->right)) + 1; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution { | ||
public: | ||
int minDepth(TreeNode* root) { | ||
queue<pair<TreeNode*, int>> nodes_and_depths; | ||
nodes_and_depths.emplace(root, 1); | ||
while (!nodes_and_depths.empty()) { | ||
auto [node, depth] = nodes_and_depths.front(); | ||
nodes_and_depths.pop(); | ||
if (!node) { | ||
continue; | ||
} | ||
if (!(node->left || node->right)) { | ||
return depth; | ||
} | ||
nodes_and_depths.emplace(node->left, depth + 1); | ||
nodes_and_depths.emplace(node->right, depth + 1); | ||
} | ||
return 0; | ||
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 == nullptr のときだけでしょうか。一番上にするのも一つかなと思います。 |
||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
私は、
のほうが好きですが、趣味の範囲でしょうか。
この問題がこれくらいで書けていたら特に問題ないと思います。