The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
Test Cases
1 2 3 4 5 6 7 8
# 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 classSolution: definvertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
import sys sys.path.insert(0, '..') from _utils.binary_tree import TreeNode
# 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 classSolution: definvertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: backup = root stack = [] while root or stack: if root: stack.append(root) root.left, root.right = root.right, root.left root = root.left # Original right child. else: root = stack.pop().right # Original left child.