The function update
in the following code tries to update a tree in place. Since Python doesn't support pass by ref so the function update
has a parameter of list
(https://realpython.com/python-pass-by-reference/).
However, n
was not updated after called update([n])
?
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def update(ns):
ns[0] = ns[0].left
n = TreeNode(1, TreeNode(2), TreeNode(3))
update([n])
# n is not updated, n should be replaced by the left child of the n (2).