Print siblings of a given Node in N | C # program for the above approach ; Structure of a node of N - ary tree ; Function to create a new node ; Function to find the siblings of the node / value ; Stores nodes level wise ; Push the root ; Continue until all levels are traversed ; Stores current node ; Enqueue all children of the current node ; If node value is found ; Print all children of current node except value as the answer ; Push the child nodes of temp into the queue ; Driver Code ; Stores root of the constructed tree ; Print siblings of Node X
using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public List < Node > child = new List < Node > ( ) ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; return temp ; } static void Siblings ( Node root , int value ) { int flag = 0 ; if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; for ( int i = 0 ; i < temp . child . Count ; i ++ ) { if ( temp . child [ i ] . key == value ) { flag = 1 ; for ( int j = 0 ; j < temp . child . Count ; j ++ ) { if ( value != temp . child [ j ] . key ) Console . Write ( temp . child [ j ] . key + " ▁ " ) ; } break ; } 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 ; } public static void Main ( String [ ] args ) { Node root = constructTree ( ) ; int X = 30 ; Siblings ( root , X ) ; } }