Skip to content

111. Minimum Depth of Binary Tree#24

Open
Ryotaro25 wants to merge 4 commits intomainfrom
problem22
Open

111. Minimum Depth of Binary Tree#24
Ryotaro25 wants to merge 4 commits intomainfrom
problem22

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/minimum-depth-of-binary-tree/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、bfs.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

nodeとdepthを保持する構造体で管理
処理の一番最後(ループの外側)にreturn 0 を入れないと
error: non-void function does not return a value in all control pathと出る
これの回避方法が今の実装でいいのか疑問です
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私C++詳しくないので的確なコメントが出来ずアレなんですが、とりあえずここ(L41)に到達し得る想定なのでしょうか?

ここら辺も参考になるかもなので貼っておきますね。
https://discord.com/channels/1084280443945353267/1226508154833993788/1227171332131786774

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これは、人間には帰納法から queue が空にならないことがいえますがコンパイラには分からないということですね。
while(1) にしてしまうのは一つでしょう。

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.

@TORUS0818
レビュー&&資料ありがとうございます。絶対に到達する認識で書いておりました。

「約束にない間違った使い方をしているのだから、そいつが悪い。驚くような動作をして、デバッグで苦労していても、私の問題ではない。」というのは好まれる態度ではないでしょう。

この部分は特に意識しないとですね。
Leetcodeではないと思いますが、いずれかの葉の左右のポインタがnullptrではなくrootを指し示していると(そもそも木ではなくなりますが)無限ループになってしまうので何らかのエラーを出したりした方が良さそうですね。

@oda
なるほどです。コンパイラ側からするとは、ツリーの1番下まで来るのか分からないからですね。
while(1)でも実装しました。
845fff1

Copy link
Copy Markdown

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

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

いいと思います。

};

public:
int minDepth(TreeNode* root) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらの解法のようにnodeが常にnullptrでないパターンでも再帰で書いてもいいかもしれません。

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.

@fhiyo
レビューありがとうございます。
こちらどういうことか分かりませんでした🙇
bfsをqueueではなく再帰で書くということでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他の実装ではノードがnullの位置まで再帰で探索していますが、この実装のように最初にrootがnullかどうかだけを確認して、nullの位置までではなくnodeが存在する位置まで探索していくのを再帰で実装してもいいかも、という意味でした。

int left_depth = minDepth(node->left);
int right_depth = minDepth(node->right);

if (node->left != nullptr && node->right == nullptr) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if (!node->left && node->right) {
と書いても良いと思います。

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.

@nodchip
レビューありがとうございます。
確かにこちらの方が短く書けますね。

こちらで書いたものも実装しました。

10^5であることを気を付ける
許容されるデータ量について理解していないので調べてみた
=>スタックメモリが確保されオーバーフローを考慮する必要がある
スタックのデフォルトサイズは8mg
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これって8MiBでしょうか??

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.

@Yoshiki-Iwasa
レビューありがとうございます。
参考にした下記の記事には8mbとあったのでmbだと思っていました。
https://stackoverflow.com/questions/2630054/does-c-limit-recursion-depth

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ミリグラムになってます

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.

@Yoshiki-Iwasa
こちらの記事を見るとメモリ関連の時はMibとありますね。ということはRamの場合の単位はMibでしょうか?🙇
https://www.majordifferences.com/2018/03/differences-between-megabyte-and.html

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.

@liquo-rice @Yoshiki-Iwasa
なるほどです。失礼しました。修正しました🙇

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@liquo-rice 資料ありがとうございます。読んでみます!

queue<NodeAndDepth> candidates;
candidates.emplace(root, 1);
while (true) {
auto [node, depth] = std::move(candidates.front());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

NodeAndDepthのメンバがpointerとintのため、copy constructor呼び出しでも十分かなと思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants