3 ms·
It's when you swap the left and right branches of every node, e.g.: def invert(node): if node is not None: left = invert(node.left)
by peterkelly 8y ago
It's when you swap the left and right branches of every node, e.g.:
def invert(node):
if node is not None:
left = invert(node.left)
right = invert(node.right)
node.left = right
node.right = left
return node
- aldanor 8y agoOr simply node.left, node.right = map(invert, (node.right, node.left))
- quietbritishjim 8y agoCoalescing those four lines onto one is fine, but writing: map(invert, (node.right, node.left)) is definitely not simpler than: invert(node.right), invert(node.left)
- peterkelly 8y agoIndeed - I just wanted to write it in a form that could easily be mapped to languages which don't support multiple simultaneous assignments :)