Given a binary tree and an integer K, the duty is to print all of the integers on the Kth stage within the tree from left to proper.
Examples:
Enter: Tree within the picture under, K = 3
Output: 4 5 6
Rationalization: All of the nodes current in stage 3 of above binary tree from left to proper are 4, 5, and 6.Enter: Tree within the picture under, K = 2
Output: 9 6
Method: The given drawback may be solved with the assistance of recursion utilizing a DFS traversal. Create a recursive operate to traverse the given tree and preserve the present stage of the node in a variable. Recursively name for the left subtree and the suitable subtree and increment the extent by 1. If the extent of the present node is the same as K, print its worth.
Under is the implementation of the above strategy:
C++
|
Time Complexity: O(N)
Auxiliary Area: O(1)