Perform an inorder traversal of a binary tree.
Example 1:
Input:
according to the question
Output:
Inorder traversal of binary tree is:
4 2 5 1 3 6
1. Node Structure:-
The code defines a structure called Node, which represents a node in a binary tree. Each Node has an integer
data, a pointer to the left child, and a pointer to the right child.
2. Constructor:-
The constructor for the Node structure initializes a node with a given value v and sets both the left and
right child pointers to NULL.
3. Inorder Traversal Function (printInorder):-
The printInorder function is a recursive function that performs an inorder traversal of the binary tree.
It takes a pointer to a Node as its argument.
The base case checks if the current node is NULL, and if so, it returns (terminates the recursion).
The function then recursively traverses the left subtree by calling printInorder on the left child.
After processing the left subtree, it prints the value of the current node.
Finally, it recursively traverses the right subtree by calling printInorder on the right child.
4. Main Function:-
In the main function, a binary tree is created.
The root node is created with the value 1.
Child nodes and their connections are established to form the binary tree structure.
The printInorder function is called with the root node as an argument to perform the inorder traversal.
The values are printed in the order specified by the inorder traversal.