inverte a binary tree code example
Example 1: invert a binary tree python
def invertTree(self, root): q = deque() q.append(root) while q: node = q.popleft() if node: node.left, node.right = node.right, node.left q.append(node.left) q.append(node.right) return root
Example 2: invert a binary tree python
def invertTree(root): stack = [root] while stack: node = stack.pop(-1) if node: node.left, node.right = node.right, node.left stack.append(node.left) stack.append(node.right) return root