📜  在二叉树中没有重复的第 K 层节点

📅  最后修改于: 2021-09-02 06:38:25             🧑  作者: Mango

给定具有N 个节点和整数K的二叉树,任务是打印二叉树K层的节点,没有重复。
例子:

Input:       
          60             --- Level 0
         /  \
       50    30          --- Level 1
      /  \   /
    80   10 40           --- Level 2

K = 1
Output: 30 50

Input:
           50            --- Level 0
          /  \
        60    70         --- Level 1
       /  \   / \
     90   40 40  20      --- Level 2
K = 2
Output: 20 40 90

方法:这个想法是在队列的帮助下使用层序遍历来遍历二叉树,如果遍历的层为 K,则将该层的所有节点存储在一个集合中,以便该层没有重复的节点.
算法:

  • 初始化一个空队列以将节点存储在一个级别。
  • 将二叉树的根节点加入队列。
  • 将 Level 初始化为 0,因为这里树的第一层应该是 0。
  • 标志初始化为 0 以检查是否达到K级别。
  • 使用 while 循环进行迭代,直到队列不为空。
    1. 查找队列的大小并以可变大小存储以仅访问当前级别的节点。
    2. 用另一个 while 循环迭代,直到 size 变量不为 0
    3. 从队列中取出一个节点并将其左右子节点放入队列中。
    4. 如果当前级别等于K,则将节点的数据添加到集合中并设置标志
  • 如果设置了标志,则中断循环以不访问更多级别,否则将当前级别增加 1。
  • 在迭代器的帮助下打印集合的元素。

举例说明:

Binary Tree -
           50            --- Level 0
          /  \
        60    70         --- Level 1
       /  \   / \
     90   40 40  20      --- Level 2 
K = 2

Initialize Queue and Set and append Root in queue

Step 1:
Queue = [50],  Set = {}, Level = 0

As current Level is not equal to K,
Deque nodes from the queue and enqueue its  child

Step 2:
Queue = [60, 70], Set = {}, Level = 1

As current level is not equal to K
Deque nodes one by one from the queue and enqueue its child

Step 3:
Queue = [90, 40, 40, 20], Set = {}, Level = 2

As the current level is equal to K
Deque all the nodes from the queue and add to the set

Set = {90, 40, 20}       

下面是该方法的实现:

C++
// C++ implementation to print the
// nodes of Kth Level without duplicates
 
#include 
using namespace std;
 
// Structure of Binary Tree node
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
// Function to create new
// Binary Tree node
struct node* newNode(int data)
{
    struct node* temp = new struct node;
    temp->data = data;
    temp->left = nullptr;
    temp->right = nullptr;
    return temp;
};
 
// Function to print the nodes
// of Kth Level without duplicates
void nodesAtKthLevel(struct node* root,
                        int k){
 
    // Condition to check if current
    // node is None
    if (root == nullptr)
        return;
         
    // Create Queue
    queue que;
 
    // Enqueue the root node
    que.push(root);
 
    // Create a set
    set s;
 
    // Level to track
    // the current level
    int level = 0;
    int flag = 0;
 
    // Iterate the queue till its not empty
    while (!que.empty()) {
 
        // Calculate the number of nodes
        // in the current level
        int size = que.size();
 
        // Process each node of the current
        // level and enqueue their left
        // and right child to the queue
        while (size--) {
            struct node* ptr = que.front();
            que.pop();
 
            // If the current level matches the
            // required level then add into set
            if (level == k) {
 
                // Flag initialized to 1
                flag = 1;
 
                // Inserting node data in set
                s.insert(ptr->data);
            }
            else {
 
                // Traverse to the left child
                if (ptr->left)
                    que.push(ptr->left);
 
                // Traverse to the right child
                if (ptr->right)
                    que.push(ptr->right);
            }
        }
 
        // Increment the variable level
        // by 1 for each level
        level++;
 
        // Break out from the loop
        // if the Kth Level is reached
        if (flag == 1)
            break;
    }
    set::iterator it;
    for (it = s.begin(); it != s.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
    struct node* root = new struct node;
 
    // Tree Construction
    root = newNode(60);
    root->left = newNode(20);
    root->right = newNode(30);
    root->left->left = newNode(80);
    root->left->right = newNode(10);
    root->right->left = newNode(40);
    int level = 1;
    nodesAtKthLevel(root, level);
 
    return 0;
}


Java
// Java implementation to print the
// nodes of Kth Level without duplicates
import java.util.*;
 
class GFG{
  
// Structure of Binary Tree node
static class node {
    int data;
    node left;
    node right;
};
  
// Function to create new
// Binary Tree node
static node newNode(int data)
{
    node temp = new node();
    temp.data = data;
    temp.left = null;
    temp.right = null;
    return temp;
};
  
// Function to print the nodes
// of Kth Level without duplicates
static void nodesAtKthLevel(node root,
                        int k){
  
    // Condition to check if current
    // node is None
    if (root == null)
        return;
          
    // Create Queue
    Queue que = new LinkedList();
  
    // Enqueue the root node
    que.add(root);
  
    // Create a set
    HashSet s = new HashSet();
  
    // Level to track
    // the current level
    int level = 0;
    int flag = 0;
  
    // Iterate the queue till its not empty
    while (!que.isEmpty()) {
  
        // Calculate the number of nodes
        // in the current level
        int size = que.size();
  
        // Process each node of the current
        // level and enqueue their left
        // and right child to the queue
        while (size-- > 0) {
            node ptr = que.peek();
            que.remove();
  
            // If the current level matches the
            // required level then add into set
            if (level == k) {
  
                // Flag initialized to 1
                flag = 1;
  
                // Inserting node data in set
                s.add(ptr.data);
            }
            else {
  
                // Traverse to the left child
                if (ptr.left!=null)
                    que.add(ptr.left);
  
                // Traverse to the right child
                if (ptr.right!=null)
                    que.add(ptr.right);
            }
        }
  
        // Increment the variable level
        // by 1 for each level
        level++;
  
        // Break out from the loop
        // if the Kth Level is reached
        if (flag == 1)
            break;
    }
    for (int it : s) {
        System.out.print(it+ " ");
    }
    System.out.println();
}
  
// Driver code
public static void main(String[] args)
{
    node root = new node();
  
    // Tree Construction
    root = newNode(60);
    root.left = newNode(20);
    root.right = newNode(30);
    root.left.left = newNode(80);
    root.left.right = newNode(10);
    root.right.left = newNode(40);
    int level = 1;
    nodesAtKthLevel(root, level);
  
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation to print the
# nodes of Kth Level without duplicates
from collections import deque
 
# A binary tree node has key, pointer to
# left child and a pointer to right child
class Node:
 
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
 
# Function to print the nodes
# of Kth Level without duplicates
def nodesAtKthLevel(root: Node, k: int):
     
    # Condition to check if current
    # node is None
    if root is None:
        return
 
    # Create Queue
    que = deque()
 
    # Enqueue the root node
    que.append(root)
 
    # Create a set
    s = set()
 
    # Level to track
    # the current level
    level = 0
    flag = 0
 
    # Iterate the queue till its not empty
    while que:
 
        # Calculate the number of nodes
        # in the current level
        size = len(que)
 
        # Process each node of the current
        # level and enqueue their left
        # and right child to the queue
        while size:
            ptr = que[0]
            que.popleft()
 
            # If the current level matches the
            # required level then add into set
            if level == k:
 
                # Flag initialized to 1
                flag = 1
 
                # Inserting node data in set
                s.add(ptr.data)
 
            else:
 
                # Traverse to the left child
                if ptr.left:
                    que.append(ptr.left)
 
                # Traverse to the right child
                if ptr.right:
                    que.append(ptr.right)
 
            size -= 1
 
        # Increment the variable level
        # by 1 for each level
        level += 1
 
        # Break out from the loop
        # if the Kth Level is reached
        if flag == 1:
            break
 
    for it in s:
        print(it, end = " ")
    print()
 
 
# Driver Code
if __name__ == "__main__":
 
    # Tree Construction
    root = Node(60)
    root.left = Node(20)
    root.right = Node(30)
    root.left.left = Node(80)
    root.left.right = Node(10)
    root.right.left = Node(40)
 
    level = 1
    nodesAtKthLevel(root, level)
 
# This code is contributed by sanjeev2552


C#
// C# implementation to print the
// nodes of Kth Level without duplicates
using System;
using System.Collections.Generic;
 
class GFG{
   
// Structure of Binary Tree node
class node {
    public int data;
    public node left;
    public node right;
};
   
// Function to create new
// Binary Tree node
static node newNode(int data)
{
    node temp = new node();
    temp.data = data;
    temp.left = null;
    temp.right = null;
    return temp;
}
   
// Function to print the nodes
// of Kth Level without duplicates
static void nodesAtKthLevel(node root,
                        int k){
   
    // Condition to check if current
    // node is None
    if (root == null)
        return;
           
    // Create Queue
    List que = new List();
   
    // Enqueue the root node
    que.Add(root);
   
    // Create a set
    HashSet s = new HashSet();
   
    // Level to track
    // the current level
    int level = 0;
    int flag = 0;
   
    // Iterate the queue till its not empty
    while (que.Count != 0) {
   
        // Calculate the number of nodes
        // in the current level
        int size = que.Count;
   
        // Process each node of the current
        // level and enqueue their left
        // and right child to the queue
        while (size-- > 0) {
            node ptr = que[0];
            que.RemoveAt(0);
   
            // If the current level matches the
            // required level then add into set
            if (level == k) {
   
                // Flag initialized to 1
                flag = 1;
   
                // Inserting node data in set
                s.Add(ptr.data);
            }
            else {
   
                // Traverse to the left child
                if (ptr.left != null)
                    que.Add(ptr.left);
   
                // Traverse to the right child
                if (ptr.right != null)
                    que.Add(ptr.right);
            }
        }
   
        // Increment the variable level
        // by 1 for each level
        level++;
   
        // Break out from the loop
        // if the Kth Level is reached
        if (flag == 1)
            break;
    }
    foreach (int it in s) {
        Console.Write(it+ " ");
    }
    Console.WriteLine();
}
   
// Driver code
public static void Main(String[] args)
{
    node root = new node();
   
    // Tree Construction
    root = newNode(60);
    root.left = newNode(20);
    root.right = newNode(30);
    root.left.left = newNode(80);
    root.left.right = newNode(10);
    root.right.left = newNode(40);
    int level = 1;
    nodesAtKthLevel(root, level);
   
}
}
 
// This code is contributed by 29AjayKumar


输出:
20 30

性能分析:

  • 时间复杂度:与上述方法一样,最坏情况下树的所有 N 个节点都访问过,因此时间复杂度为O(N)
  • 空间复杂度:在最坏的情况下,在树的最底层,它的最大节点数为 2 H-1 ,其中 H 是二叉树的高度,则二叉树的空间复杂度为O(2 H-1 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live