Skip to content

【Grind75Hard】2問目297. Serialize and Deserialize Binary Tree#62

Open
shining-ai wants to merge 2 commits intomainfrom
grind75_02
Open

【Grind75Hard】2問目297. Serialize and Deserialize Binary Tree#62
shining-ai wants to merge 2 commits intomainfrom
grind75_02

Conversation

@shining-ai
Copy link
Copy Markdown
Owner

"""
if not root:
return ""
serial_tree = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

serialized_treeでどうでしょう?

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.

自分で気づけないので、変数名の指摘は非常にありがたいです。

while node_queue:
node = node_queue.popleft()
if not node:
serial_tree.append("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.

static memberとして、NULL_SYMBOL = "#"などを定義してはどうですか?

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.

leetcodeだと定数を使うのを忘れてしまいがちなので、意識的に使うようにします。

node.right = helper_deserialize(data)
return node

root = helper_deserialize(deque(data.split(",")))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

indexを管理したくなかったら、

it = iter(data.split(","))
next(it)

とする方法もあります。

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.

イテレータを使う発想がありませんでした。
dequeする必要がなかったので、分かりやすくなった気がします。

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.

2 participants