Skip to content

Latest commit

 

History

History
44 lines (30 loc) · 1.2 KB

_226. Invert Binary Tree.md

File metadata and controls

44 lines (30 loc) · 1.2 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 03, 2024

Last updated : July 03, 2024


Related Topics : Tree, Depth-First Search, Breadth-First Search, Binary Tree

Acceptance Rate : 78.64 %


Solutions

Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root or (not root.left and not root.right) :
            return root

        root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
        return root