- categories: Code, Interview Question, leetcode, Easy
- source: https://leetcode.com/problems/invert-binary-tree/
- topics: Tree
Given the root
of a binary tree, invert the tree, and return its root.
Depth-first search
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root and (root.left or root.right):
temp = root.left
root.left = root.right
root.right = temp
self.invertTree(root.left)
self.invertTree(root.right)
return root