-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】2問目297. Serialize and Deserialize Binary Tree #62
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # BFSで実装 | ||
| class Codec: | ||
| def serialize(self, root): | ||
| """Encodes a tree to a single string. | ||
|
|
||
| :type root: TreeNode | ||
| :rtype: str | ||
| """ | ||
| if not root: | ||
| return "" | ||
| serial_tree = [] | ||
| node_queue = deque([root]) | ||
| while node_queue: | ||
| node = node_queue.popleft() | ||
| if not node: | ||
| serial_tree.append("null") | ||
|
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. static memberとして、NULL_SYMBOL = "#"などを定義してはどうですか?
Owner
Author
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. leetcodeだと定数を使うのを忘れてしまいがちなので、意識的に使うようにします。 |
||
| continue | ||
| serial_tree.append(str(node.val)) | ||
| node_queue.append(node.left) | ||
| node_queue.append(node.right) | ||
| return ",".join(serial_tree) | ||
|
|
||
| def deserialize(self, data): | ||
| """Decodes your encoded data to tree. | ||
|
|
||
| :type data: str | ||
| :rtype: TreeNode | ||
| """ | ||
| if not data: | ||
| return None | ||
| data_queue = deque(list(data.split(","))) | ||
| value = data_queue.popleft() | ||
| root = TreeNode(value) | ||
| node_queue = deque([root]) | ||
| while data_queue: | ||
| left_value = data_queue.popleft() | ||
| right_value = data_queue.popleft() | ||
| node = node_queue.popleft() | ||
| if not left_value == "null": | ||
| node.left = TreeNode(left_value) | ||
| node_queue.append(node.left) | ||
| if not right_value == "null": | ||
| node.right = TreeNode(right_value) | ||
| node_queue.append(node.right) | ||
| return root | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # DFSで実装 | ||
| class Codec: | ||
| def serialize(self, root): | ||
| """Encodes a tree to a single string. | ||
|
|
||
| :type root: TreeNode | ||
| :rtype: str | ||
| """ | ||
|
|
||
| serial_tree = [] | ||
|
|
||
| def helper_serialize(node): | ||
| if not node: | ||
| serial_tree.append("null") | ||
| return | ||
| serial_tree.append(str(node.val)) | ||
| helper_serialize(node.left) | ||
| helper_serialize(node.right) | ||
|
|
||
| helper_serialize(root) | ||
| return ",".join(serial_tree) | ||
|
|
||
| def deserialize(self, data): | ||
| """Decodes your encoded data to tree. | ||
|
|
||
| :type data: str | ||
| :rtype: TreeNode | ||
| """ | ||
|
|
||
| def helper_deserialize(data): | ||
| value = data.popleft() | ||
| if value == "null": | ||
| return None | ||
| node = TreeNode(value) | ||
| node.left = helper_deserialize(data) | ||
| node.right = helper_deserialize(data) | ||
| return node | ||
|
|
||
|
|
||
| root = helper_deserialize(deque(data.split(","))) | ||
| return root | ||
|
|
||
|
|
||
| # Your Codec object will be instantiated and called as such: | ||
| # ser = Codec() | ||
| # deser = Codec() | ||
| # ans = deser.deserialize(ser.serialize(root)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| class Codec: | ||
| def serialize(self, root): | ||
| """Encodes a tree to a single string. | ||
|
|
||
| :type root: TreeNode | ||
| :rtype: str | ||
| """ | ||
|
|
||
| serial_tree = [] | ||
|
|
||
| def helper_serialize(node): | ||
| if not node: | ||
| serial_tree.append("null") | ||
| return | ||
| serial_tree.append(str(node.val)) | ||
| helper_serialize(node.left) | ||
| helper_serialize(node.right) | ||
|
|
||
| helper_serialize(root) | ||
| return ",".join(serial_tree) | ||
|
|
||
| def deserialize(self, data): | ||
| """Decodes your encoded data to tree. | ||
|
|
||
| :type data: str | ||
| :rtype: TreeNode | ||
| """ | ||
|
|
||
| def helper_deserialize(data): | ||
| value = data.popleft() | ||
| if value == "null": | ||
| return None | ||
| node = TreeNode(value) | ||
| node.left = helper_deserialize(data) | ||
| node.right = helper_deserialize(data) | ||
| return node | ||
|
|
||
| root = helper_deserialize(deque(data.split(","))) | ||
|
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. indexを管理したくなかったら、 とする方法もあります。
Owner
Author
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. イテレータを使う発想がありませんでした。 |
||
| return root | ||
|
|
||
|
|
||
| # Your Codec object will be instantiated and called as such: | ||
| # ser = Codec() | ||
| # deser = Codec() | ||
| # ans = deser.deserialize(ser.serialize(root)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # NULL_SYMBOLを作成 | ||
| # helper_deserializeの引数をイテレータに変更 | ||
| # 変数名を修正 | ||
| class Codec: | ||
| NULL_SYMBOL = "#" | ||
|
|
||
| def serialize(self, root): | ||
| """Encodes a tree to a single string. | ||
|
|
||
| :type root: TreeNode | ||
| :rtype: str | ||
| """ | ||
|
|
||
| serialized_tree = [] | ||
|
|
||
| def helper_serialize(node): | ||
| if not node: | ||
| serialized_tree.append(self.NULL_SYMBOL) | ||
| return | ||
| serialized_tree.append(str(node.val)) | ||
| helper_serialize(node.left) | ||
| helper_serialize(node.right) | ||
|
|
||
| helper_serialize(root) | ||
| return ",".join(serialized_tree) | ||
|
|
||
| def deserialize(self, data): | ||
| """Decodes your encoded data to tree. | ||
|
|
||
| :type data: str | ||
| :rtype: TreeNode | ||
| """ | ||
|
|
||
| def helper_deserialize(data): | ||
| value = next(data) | ||
| if value == self.NULL_SYMBOL: | ||
| return None | ||
| node = TreeNode(value) | ||
| node.left = helper_deserialize(data) | ||
| node.right = helper_deserialize(data) | ||
| return node | ||
|
|
||
| root = helper_deserialize(iter(data.split(","))) | ||
| return root | ||
|
|
||
|
|
||
| # Your Codec object will be instantiated and called as such: | ||
| # ser = Codec() | ||
| # deser = Codec() | ||
| # ans = deser.deserialize(ser.serialize(root)) |
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.
serialized_treeでどうでしょう?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.
自分で気づけないので、変数名の指摘は非常にありがたいです。