Skip to content

297. Serialize and Deserialize Binary Tree - #119

Open
kazuki-official wants to merge 1 commit into
mainfrom
297-serialize-and-deserialize-binary-tree
Open

297. Serialize and Deserialize Binary Tree#119
kazuki-official wants to merge 1 commit into
mainfrom
297-serialize-and-deserialize-binary-tree

Conversation

@kazuki-official

Copy link
Copy Markdown
Owner

Comment thread memo.md

auto write_to_stream = [&oss](auto self, TreeNode* node) -> void {
if (node == NULL) {
oss << NO_NODE << " ";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

INT_MAXが入っていると文字数が長く無駄が大きそうですね。
ノードの値がINT_MAXになりうるなら、NULLと区別できなくなりますね。

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.

たしかに、Serializeをしたい意図を考えると, 文字数が長くなるのは避けた方がいいかもしれません。

Comment thread memo.md
std::ostringstream oss;

auto write_to_stream = [&oss](auto self, TreeNode* node) -> void {
if (node == NULL) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nullptrとNULLの違いを調べてみると良さそうです。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.

NULLは実態としては0を表すので意図しない動作が起こることがありますね。
今回はleetcodeのTreeNodeの定義でNULLが使われていたのでNULLを使用していました。

Comment thread memo.md
* `ostringstream`でserializeするときに空白を入れておくことで, 上記の特徴を生かせる
* C++のスタイルガイドに, ラムダ式はキャプチャするものを明示した方が良いと書いてあったので明示した.
* 再帰関数の場合もそうなのかはあまり自信がないが。。。
* つまり, `std::function`を使用して, キャプチャに`[&]`を使う方が見やすい可能性もあると思った.

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
Collaborator

Choose a reason for hiding this comment

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

self(self,...)となってしまうの若干の見にくさの改善よりは、深刻なバグの可能性を防ぐ方が大切そうですね

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants