Print all Nodes of given Binary Tree at the Kth Level
Last Updated :
29 Mar, 2022
Given a binary tree and an integer K, the task is to print all the integers at the Kth level in the tree from left to right.
Examples:
Input: Tree in the image below, K = 3
Output: 4 5 6
Explanation: All the nodes present in level 3 of above binary tree from left to right are 4, 5, and 6.
Input: Tree in the image below, K = 2
Output: 9 6
Approach: The given problem can be solved with the help of recursion using a DFS traversal. Create a recursive function to traverse the given tree and maintain the current level of the node in a variable. Recursively call for the left subtree and the right subtree and increment the level by 1. If the level of the current node is equal to K, print its value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
void printNodes(Node* root, int level, int K)
{
if (root == NULL) {
return ;
}
printNodes(root->left, level + 1, K);
printNodes(root->right, level + 1, K);
if (K == level) {
cout << root->data << " " ;
}
}
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int main()
{
Node* root = newNode(3);
root->left = newNode(9);
root->right = newNode(6);
root->left->left = newNode(11);
int K = 2;
printNodes(root, 1, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node left, right;
};
static void printNodes(Node root, int level, int K)
{
if (root == null ) {
return ;
}
printNodes(root.left, level + 1 , K);
printNodes(root.right, level + 1 , K);
if (K == level) {
System.out.print(root.data+ " " );
}
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static void main(String[] args)
{
Node root = newNode( 3 );
root.left = newNode( 9 );
root.right = newNode( 6 );
root.left.left = newNode( 11 );
int K = 2 ;
printNodes(root, 1 , K);
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self .left = None
self .right = None
def printNodes(root, level, K):
if (root = = None ):
return
printNodes(root.left, level + 1 , K)
printNodes(root.right, level + 1 , K)
if (K = = level):
print (root.data, end = " " )
root = Node( 3 )
root.left = Node( 9 )
root.right = Node( 6 )
root.left.left = Node( 11 )
K = 2
printNodes(root, 1 , K)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public class Node {
public int data;
public Node left, right;
};
static void printNodes(Node root, int level, int K) {
if (root == null ) {
return ;
}
printNodes(root.left, level + 1, K);
printNodes(root.right, level + 1, K);
if (K == level) {
Console.Write(root.data + " " );
}
}
static Node newNode( int data) {
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static void Main(String[] args)
{
Node root = newNode(3);
root.left = newNode(9);
root.right = newNode(6);
root.left.left = newNode(11);
int K = 2;
printNodes(root, 1, K);
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .left = null ;
this .right = null ;
}
}
function printNodes(root, level, K)
{
if (root == null ) {
return ;
}
printNodes(root.left, level + 1, K);
printNodes(root.right, level + 1, K);
if (K == level) {
document.write(root.data + " " );
}
}
let root = new Node(3);
root.left = new Node(9);
root.right = new Node(6);
root.left.left = new Node(11);
let K = 2;
printNodes(root, 1, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...