擅长:python、mysql、java
<p>在Python中有两大类对象:不可变对象和可变对象。必须了解它们之间的区别:</p>
<pre><code>a = [] # a list, lists are mutable
b = a # b and a now reference the same object
b.append(1) # change b and the change will be in-place
print(a) # since a references the same object
# [1]
a = 1 # an int, ints are immutable
b = a # b and a may well reference the same object, but
b += 1 # since the object cannot change a new object is bound to b
print(a) # leaving a unaffected
# 1
</code></pre>
<p>类似地,如果您将一个列表传递给一个函数,而该函数更改了该列表,但没有显式地返回它,那么调用方仍然可以看到这些更改,事实上,任何持有该列表引用的人都可以看到这些更改。有人说这是副作用。您正在序列化程序中使用此技术。你知道吗</p>
<p>如果将<code>index</code>这样的不可变对象传递给函数,并在函数中对其进行操作,则原始对象不会更改。函数中的名称只是绑定到调用方不可见的新对象,<em>除非显式返回它们。你知道吗</p>
<p>因此,要修复反序列化程序,请尝试返回子树和当前索引,如下所示</p>
<pre><code>return root, index
</code></pre>
<p>所以打电话的人可以像这样更新他们的</p>
<pre><code>root.left, index = deserialize_helper(tree_list, index)
</code></pre>