297. Serialize and Deserialize Binary Tree - #119
Open
kazuki-official wants to merge 1 commit into
Open
Conversation
liquo-rice
reviewed
Jul 11, 2026
|
|
||
| auto write_to_stream = [&oss](auto self, TreeNode* node) -> void { | ||
| if (node == NULL) { | ||
| oss << NO_NODE << " "; |
There was a problem hiding this comment.
INT_MAXが入っていると文字数が長く無駄が大きそうですね。
ノードの値がINT_MAXになりうるなら、NULLと区別できなくなりますね。
Owner
Author
There was a problem hiding this comment.
たしかに、Serializeをしたい意図を考えると, 文字数が長くなるのは避けた方がいいかもしれません。
| std::ostringstream oss; | ||
|
|
||
| auto write_to_stream = [&oss](auto self, TreeNode* node) -> void { | ||
| if (node == NULL) { |
There was a problem hiding this comment.
nullptrとNULLの違いを調べてみると良さそうです。nullptrが好まれます。
Owner
Author
There was a problem hiding this comment.
NULLは実態としては0を表すので意図しない動作が起こることがありますね。
今回はleetcodeのTreeNodeの定義でNULLが使われていたのでNULLを使用していました。
nodchip
reviewed
Jul 14, 2026
| * `ostringstream`でserializeするときに空白を入れておくことで, 上記の特徴を生かせる | ||
| * C++のスタイルガイドに, ラムダ式はキャプチャするものを明示した方が良いと書いてあったので明示した. | ||
| * 再帰関数の場合もそうなのかはあまり自信がないが。。。 | ||
| * つまり, `std::function`を使用して, キャプチャに`[&]`を使う方が見やすい可能性もあると思った. |
There was a problem hiding this comment.
ラムダ式内で、本来変更する意図のない、外側の変数を変更してしまう危険性があると思いました。
Collaborator
There was a problem hiding this comment.
self(self,...)となってしまうの若干の見にくさの改善よりは、深刻なバグの可能性を防ぐ方が大切そうですね
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/