0226 Invert Binary Tree
Solved at: 2022-07-26
Question
Given the root of a binary tree, invert the tree, and return its root.
Solution
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 = rightclass Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return root.left, root.right = root.right, root.left if root.left: self.invertTree(root.left) if root.right: self.invertTree(root.right) return rootThat was simple...
Results
Runtime
- 30 ms, faster than 95.71% of Python3 online submissions for Invert Binary Tree.
Memory Usage
- 13.9 MB, less than 11.66% of Python3 online submissions for Invert Binary Tree.