1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return root.left, root.right = root.right, root.left self.invertTree(root.left) self.invertTree(root.right) return root
class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return stack = [root] while stack: cur = stack.pop() cur.left, cur.right = cur.right, cur.left if cur.left: stack.append(cur.left) if cur.right: stack.append(cur.right) return root
|