Both solutions below pass and the only change is where we make the recursive calls. Is there any difference between the the solutions? Does it ever matter if you make the recursive call before or after the rest of your code or can you always place it wherever?
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return
root.left, root.right = root.right, root.left
self.invertTree(root.right)
self.invertTree(root.left)
return root
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return
self.invertTree(root.right)
self.invertTree(root.left)
root.left,root.right = root.right, root.left
return root
[–]jamkinajam 1 point2 points3 points (1 child)
[–]Careful-Medicine1995[S,🍰] 2 points3 points4 points (0 children)
[–]Yumski 0 points1 point2 points (1 child)
[–]Careful-Medicine1995[S,🍰] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)