Question
Binary Tree Inversion
You are given the root node of a binary tree. Your task is to invert the binary tree by swapping the left and right children of every node in the tree.
Input
User Task
Since this is a functional problem, you do not need to handle input or output. Your task is to complete the function invert_binary_tree(), which takes the root node of the binary tree as its parameter.
Custom Input
The first line of input contains a positive integer N, representing the number of nodes in the tree.
The next line consists of space-separated integers denoting the level order traversal of the tree, where non-negative values represent node values, and -1 indicates a null node.
Note: Once a node is marked as -1, no further information about its children is provided.
Since this is a functional problem, you do not need to handle input or output. Your task is to complete the function invert_binary_tree(), which takes the root node of the binary tree as its parameter.
Custom Input
The first line of input contains a positive integer N, representing the number of nodes in the tree.
The next line consists of space-separated integers denoting the level order traversal of the tree, where non-negative values represent node values, and -1 indicates a null node.
Note: Once a node is marked as -1, no further information about its children is provided.
Output
Return the root of the inverted binary tree.
Custom Output
Inorder Traversal of the inverted binary tree will be printed by the driver code.
Custom Output
Inorder Traversal of the inverted binary tree will be printed by the driver code.
Example
Input
6
2 4 10 6 5 11
Output
10 11 2 5 4 6
Explanation
The above given input parameters represents the following binary tree:
After inverting the tree by swapping the left and right children of every node, the inorder traversal of the resulting tree is:
And the inorder traversal of the inverted binary tree is [10, 11, 2, 5, 4, 6]
6
2 4 10 6 5 11
Output
10 11 2 5 4 6
Explanation
The above given input parameters represents the following binary tree:
After inverting the tree by swapping the left and right children of every node, the inorder traversal of the resulting tree is:
And the inorder traversal of the inverted binary tree is [10, 11, 2, 5, 4, 6]