📌  相关文章
📜  在 N 叉树中打印给定节点的兄弟节点

📅  最后修改于: 2021-09-06 05:58:43             🧑  作者: Mango

给定一个N 叉树和一个元素X ,任务是打印值为X的节点的兄弟节点。

例子:

方法:按照下面给出的步骤解决问题:

  1. 在给定的N叉树上执行层序遍历。
  2. 初始化队列q以进行层序遍历。
  3. 对于遇到的每个节点,将其所有子节点推入队列。
  4. 在将当前节点的孩子推入队列时,检查:这些孩子中的任何一个是否等于给定的值X。如果发现为真,则打印除X之外的所有节点,这些节点是当前节点的子节点作为所需答案。
  5. 否则,继续遍历树。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Structure of a node of N-ary tree
struct Node {
    int key;
    vector child;
};
 
// Function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    return temp;
}
 
// Function to find the siblings
// of the node value
void Siblings(Node* root, int value)
{
    int flag = 0;
 
    if (root == NULL)
        return;
 
    // Stores nodes level wise
    queue q;
 
    // Push the root
    q.push(root);
 
    // Continue until all levels
    // are traversed
    while (!q.empty()) {
 
        // Stores current node
        Node* temp = q.front();
        q.pop();
 
        // Enqueue all children of the current node
        for (int i = 0; i < temp->child.size(); i++) {
 
            // If node value is found
            if (temp->child[i]->key == value) {
 
                flag = 1;
 
                // Print all children of current node
                // except value as the answer
                for (int j = 0; j < temp->child.size();
                     j++) {
 
                    if (value
                        != temp->child[j]->key)
                        cout << temp->child[j]->key
                             << " ";
                }
                break;
            }
 
            // Push the child nodes
            // of temp into the queue
            q.push(temp->child[i]);
        }
    }
 
    if (flag == 0)
        cout << "No siblings!!";
}
 
Node* constructTree()
{
    Node* root = newNode(10);
    (root->child).push_back(newNode(20));
 
    (root->child).push_back(newNode(30));
 
    (root->child).push_back(newNode(40));
 
    (root->child[0]->child).push_back(newNode(50));
 
    (root->child[0]->child).push_back(newNode(60));
 
    (root->child[1]->child).push_back(newNode(70));
 
    (root->child[1]->child).push_back(newNode(80));
 
    (root->child[2]->child).push_back(newNode(90));
 
    (root->child[2]->child).push_back(newNode(100));
 
    (root->child[2]->child).push_back(newNode(110));
 
    return root;
}
 
// Driver Code
int main()
{
 
    // Stores root of the
    // constructed tree
    Node* root = constructTree();
 
    int X = 30;
    // Print siblings of Node X
    Siblings(root, X);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Structure of a node
// of N-ary tree
static class Node
{
  int key;
  Vector child =
         new Vector<>();
};
 
// Function to create a
// new node
static Node newNode(int key)
{
  Node temp = new Node();
  temp.key = key;
  return temp;
}
 
// Function to find the
// siblings of the node
/// value
static void Siblings(Node root,
                     int value)
{
  int flag = 0;
 
  if (root == null)
    return;
 
  // Stores nodes level wise
  Queue q =
    new LinkedList<>();
 
  // Push the root
  q.add(root);
 
  // Continue until all
  // levels are traversed
  while (!q.isEmpty())
  {
    // Stores current node
    Node temp = q.peek();
    q.remove();
 
    // Enqueue all children
    // of the current node
    for (int i = 0;
         i < temp.child.size();
         i++)
    {
      // If node value is found
      if (temp.child.get(i).key ==
          value)
      {
        flag = 1;
 
        // Print all children of current
        // node
        // except value as the answer
        for (int j = 0;
             j < temp.child.size();
             j++)
        {
          if (value !=
              temp.child.get(j).key)
            System.out.print(
            temp.child.get(j).key + " ");
        }
        break;
      }
 
      // Push the child nodes
      // of temp into the queue
      q.add(temp.child.get(i));
    }
  }
 
  if (flag == 0)
    System.out.print("No siblings!!");
}
 
static Node constructTree()
{
  Node root = newNode(10);
   
  (root.child).add(newNode(20));
  (root.child).add(newNode(30));
  (root.child).add(newNode(40));
  (root.child.get(0).child).add(newNode(50));
  (root.child.get(0).child).add(newNode(60));
  (root.child.get(1).child).add(newNode(70));
  (root.child.get(1).child).add(newNode(80));
  (root.child.get(2).child).add(newNode(90));
  (root.child.get(2).child).add(newNode(100));
  (root.child.get(2).child).add(newNode(110));
 
  return root;
}
 
// Driver Code
public static void main(String[] args)
{
  // Stores root of the
  // constructed tree
  Node root = constructTree();
 
  int X = 30;
  // Print siblings of Node X
  Siblings(root, X);
}
}
 
// This code is contributed by Rajput-Ji


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Structure of a node
// of N-ary tree
public class Node
{
  public int key;
  public List child =
         new List();
};
 
// Function to create a
// new node
static Node newNode(int key)
{
  Node temp = new Node();
  temp.key = key;
  return temp;
}
 
// Function to find the
// siblings of the node
/// value
static void Siblings(Node root,
                     int value)
{
  int flag = 0;
 
  if (root == null)
    return;
 
  // Stores nodes level
  // wise
  Queue q =
        new Queue();
 
  // Push the root
  q.Enqueue(root);
 
  // Continue until all
  // levels are traversed
  while (q.Count != 0)
  {
    // Stores current node
    Node temp = q.Peek();
    q.Dequeue();
 
    // Enqueue all children
    // of the current node
    for (int i = 0;
             i < temp.child.Count; i++)
    {
      // If node value is found
      if (temp.child[i].key == value)
      {
        flag = 1;
 
        // Print all children of
        // current node except value
        // as the answer
        for (int j = 0;
             j < temp.child.Count; j++)
        {
          if (value != temp.child[j].key)
            Console.Write(temp.child[j].key + " ");
        }
        break;
      }
 
      // Push the child nodes
      // of temp into the queue
      q.Enqueue(temp.child[i]);
    }
  }
 
  if (flag == 0)
    Console.Write("No siblings!!");
}
 
static Node constructTree()
{
  Node root = newNode(10);
 
  (root.child).Add(newNode(20));
  (root.child).Add(newNode(30));
  (root.child).Add(newNode(40));
  (root.child[0].child).Add(newNode(50));
  (root.child[0].child).Add(newNode(60));
  (root.child[1].child).Add(newNode(70));
  (root.child[1].child).Add(newNode(80));
  (root.child[2].child).Add(newNode(90));
  (root.child[2].child).Add(newNode(100));
  (root.child[2].child).Add(newNode(110));
 
  return root;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Stores root of the
  // constructed tree
  Node root = constructTree();
 
  int X = 30;
   
  // Print siblings of Node X
  Siblings(root, X);
}
}
 
// This code is contributed by Rajput-Ji


输出:
20 40








时间复杂度: O(N 2 )
辅助空间: O(N)

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