原文:

给定一个 和一个元素 x ,任务是用值 x 打印节点的兄弟节点。

如果两个节点位于同一级别并且具有相同的父节点,则它们被视为兄弟节点

示例:

输入: x = 100

输出:90 110 t3】说明:值为 90、100、110 的节点有相同的父节点,即值为 40 的节点。因此,节点 90 和 110 是给定节点 x( = 100)的兄弟节点。

输入: x = 30

输出:20 40 t3】说明:值为 20、30、40 的节点有相同的父节点,即值为 10 的节点。因此,节点 20 和 40 是给定节点 x( = 30)的兄弟节点。

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

  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

java 描述语言


output: 

20 40

时间复杂度:o(n2) 辅助空间: o(n)