Introduction
Inverting a binary tree means swapping all left and right node pairs. This is a common problem in computer science and is often used as an interview question.
Inverting a binary tree means swapping all left and right node pairs. This is a common problem in computer science and is often used as an interview question.
Given a binary tree, write a function to invert the tree. The function should take the root node of the tree as input and return the new root node of the inverted tree.
To solve this problem, you need to meet the following requirements:
Suppose we have the following binary tree:
5
/ \
2 7
/ \ / \
1 3 6 9
After inverting the tree, we should get:
5
/ \
7 2
/ \ / \
9 6 3 1
Inverting a binary tree is a common problem in computer science. By swapping all left and right node pairs, we can create a new tree that is the mirror image of the original tree. To solve this problem, we need to have a Node class that represents a node in the binary tree, and we need to handle invalid inputs gracefully.