tags: leetcode

226. Invert Binary Tree

Tips:

  • dfs with reverting the two node (left and right).
  • Remember to return the node.

Code

class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        if root == None:
            return
        root.left, root.right = root.right, root.left
        self.invertTree(root.left)
        self.invertTree(root.right)
        return root