# Print all Nodes of given Binary Tree on the Kth Stage

0
83 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++

 `#embrace ` `utilizing` `namespace` `std;` ` `  `struct` `Node {` `    ``int` `knowledge;` `    ``struct` `Node *left, *proper;` `};` ` `  `void` `printNodes(Node* root, ``int` `stage, ``int` `K)` `{` `    ` `    ``if` `(root == NULL) {` `        ``return``;` `    ``}` ` `  `    ` `    ` `    ``printNodes(root->left, stage + 1, K);` ` `  `    ` `    ` `    ``printNodes(root->proper, stage + 1, K);` ` `  `    ` `    ` `    ``if` `(K == stage) {` `        ``cout << root->knowledge << ``" "``;` `    ``}` `}` ` `  `Node* newNode(``int` `knowledge)` `{` `    ``Node* temp = ``new` `Node;` `    ``temp->knowledge = knowledge;` `    ``temp->left = temp->proper = NULL;` `    ``return` `temp;` `}` ` `  `int` `primary()` `{` `    ``Node* root = newNode(3);` `    ``root->left = newNode(9);` `    ``root->proper = newNode(6);` `    ``root->left->left = newNode(11);` `    ``int` `K = 2;` ` `  `    ``printNodes(root, 1, K);` `    ``return` `0;` `}`

Time Complexity: O(N)
Auxiliary Area: O(1)