Serializing Tree into nested list using python <p>I have a binary tree class like this:</p> <pre><code>class BinaryTree: def __init__(self, data, left=None, right=None): self.data = data self.left = left self.right = right </code></pre> <p>Now I'm facing a task to serialize this structure in to a nested list. BTW, I have a left-to-right traversal function in mind:</p> <pre><code>def binary_tree(tree): if tree: for node_data in binary_tree(tree.left): yield node_data for node_data in binary_tree(tree.right): yield node_data </code></pre> <p>Or there is a general way to serialize it into mixed nested structure? For example, {[]}, or [{}]?</p>