在第 K 层打印给定二叉树的所有节点
给定一棵二叉树和一个整数 K,任务是从左到右打印树中第K层的所有整数。
例子:
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
方法:给定的问题可以通过使用 DFS 遍历的递归来解决。创建一个递归函数来遍历给定的树并在变量中维护节点的当前级别。递归调用左子树和右子树并将级别增加1 。如果当前节点的级别等于K ,则打印其值。
下面是上述方法的实现:
C++
// C++ Program of the above approach
#include
using namespace std;
// A Binary Tree Node
struct Node {
int data;
struct Node *left, *right;
};
// Recursive function to print all
// nodes of a Binary Tree at a
// given level using DFS traversal
void printNodes(Node* root, int level, int K)
{
// Base Case
if (root == NULL) {
return;
}
// Recursive Call for
// the left subtree
printNodes(root->left, level + 1, K);
// Recursive Call for
// the right subtree
printNodes(root->right, level + 1, K);
// If current level is
// the required level
if (K == level) {
cout << root->data << " ";
}
}
// Function to create a new tree node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Driver Code
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
// Java Program of the above approach
import java.util.*;
class GFG{
// A Binary Tree Node
static class Node {
int data;
Node left, right;
};
// Recursive function to print all
// nodes of a Binary Tree at a
// given level using DFS traversal
static void printNodes(Node root, int level, int K)
{
// Base Case
if (root == null) {
return;
}
// Recursive Call for
// the left subtree
printNodes(root.left, level + 1, K);
// Recursive Call for
// the right subtree
printNodes(root.right, level + 1, K);
// If current level is
// the required level
if (K == level) {
System.out.print(root.data+ " ");
}
}
// Function to create a new tree node
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// Driver Code
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);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python code for the above approach
class Node:
def __init__(self, d):
self.data = d
self.left = None
self.right = None
# Recursive function to print all
# nodes of a Binary Tree at a
# given level using DFS traversal
def printNodes(root, level, K):
# Base Case
if (root == None):
return
# Recursive Call for
# the left subtree
printNodes(root.left, level + 1, K)
# Recursive Call for
# the right subtree
printNodes(root.right, level + 1, K)
# If current level is
# the required level
if (K == level):
print(root.data, end=" ")
# Driver Code
root = Node(3)
root.left = Node(9)
root.right = Node(6)
root.left.left = Node(11)
K = 2
printNodes(root, 1, K)
# This code is contributed by gfgking
C#
// C# Program of the above approach
using System;
using System.Collections.Generic;
public class GFG {
// A Binary Tree Node
public class Node {
public int data;
public Node left, right;
};
// Recursive function to print all
// nodes of a Binary Tree at a
// given level using DFS traversal
static void printNodes(Node root, int level, int K) {
// Base Case
if (root == null) {
return;
}
// Recursive Call for
// the left subtree
printNodes(root.left, level + 1, K);
// Recursive Call for
// the right subtree
printNodes(root.right, level + 1, K);
// If current level is
// the required level
if (K == level) {
Console.Write(root.data + " ");
}
}
// Function to create a new tree node
static Node newNode(int data) {
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// Driver Code
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);
}
}
// This code is contributed by Rajput-Ji
Javascript
输出
9 6
时间复杂度:O(N)
辅助空间:O(1)