5 ms·
Btw, Python has nifty multiple assignment, so you can write the swap simply as: root.left, root.right = (self.invertTree(root.right),
by sltkr 11y ago
Btw, Python has nifty multiple assignment, so you can write the swap simply as:
root.left, root.right = (self.invertTree(root.right),
self.invertTree(root.left))
(Parentheses added to break over two lines.)
- aidos 11y agoThat was my immediate thought too. It's nice to think of those 2 operations as an atomic unit.
- Freaky 11y agoSame in Ruby, but it doesn't need the parens to cross lines: root.left, root.right = invert_tree(root.right), invert_tree(root.left)
- deleted 11y ago[deleted]
- TheLoneWolfling 11y agoI don't like that. I see it as potentially causing more far too many potential problems for such a niece feature. Especially as Ruby has automatic unpacking of function returns, yes? So something along the lines of: a,b = function_returning_two_things(), function_returning_one_thing() could cause bugs.
- Freaky 11y agoThat's certainly possible - this would get you 'a' containing an array from the first function and 'b' with the result of the last. Can't off the top of my head recall actually seeing such a bug. I totally appreciate the desire to reduce the potential for errors from typos, but that ship has kind of already sailed with either language. I guess it just depends how far you're willing to let it go. Yay choice. Testing will catch it anyway, right? :/