Category: n-ary-tree

Maximize difference between pair of nodes in a given rooted tree such that one node is ancestor of another

#include using namespace std;  vector tree;vector visited;vector ancestorNum;  int maxDiff = INT_MIN;  void dfs(int src, int val, vector& W){          visited[src] = true;          for (auto neighbour : tree[src]) {                                  if (!visited[neighbour]            && (ancestorNum[neighbour]                > ancestorNum[src])) {                          maxDiff = max(                val – W[neighbour – 1],                maxDiff);                          dfs(neighbour, val, W);        }    }}  void bfs(int src, int N){            visited.assign(N, false);          queue q;              ancestorNum[src] = 0;          visited[src] = true;          q.push(src);          while (!q.empty()) {                  int cur = q.front();        q.pop();                  for (auto neighbour : tree[cur]) {                                      if […]

Continue Reading

Minimum distance between two given nodes in an N-ary tree

#include using namespace std;  struct Node {    int val;    vector child;};  Node* newNode(int key){    Node* temp = new Node;    temp->val = key;    return temp;}  bool flag;  void findPath(Node* root, int key,              vector& arr){    if (!root)        return;    arr.push_back(root->val);        if (root->val == key) {        flag = 1;        return;    }        for (int i = 0; i < root->child.size(); i++) {          findPath(root->child[i], key, arr);                  if (flag == 1)            return;    }      arr.pop_back();    return;}  void findMinDist(Node* root, int A, int B){    if (root == NULL)        return;    int val = […]

Continue Reading

Kth largest element in an N-array Tree

  #include using namespace std;  class Node {public:    int data;    vector childs;};  int largestELe = INT_MIN;  void largestEleUnderRange(    Node* root, int data){            if (root->data < data) {        largestELe = max(root->data,                         largestELe);    }          for (Node* child : root->childs) {                  largestEleUnderRange(child, data);    }}  void KthLargestElement(Node* root,                       int K){            int ans = INT_MAX;          for (int i = 0; i < K; i++) {                                  largestEleUnderRange(root, ans);                          ans = largestELe;        largestELe = INT_MIN;    }          cout data = data;          return temp;}  int main(){                                        Node* root = newNode(10);    (root->childs).push_back(newNode(2));    (root->childs).push_back(newNode(34));    (root->childs).push_back(newNode(56));    (root->childs).push_back(newNode(100));    (root->childs[0]->childs).push_back(newNode(77));    (root->childs[0]->childs).push_back(newNode(88));    (root->childs[2]->childs).push_back(newNode(1));    (root->childs[3]->childs).push_back(newNode(7));    (root->childs[3]->childs).push_back(newNode(8));    (root->childs[3]->childs).push_back(newNode(9));      int […]

Continue Reading

Minimum time required to color all edges of a Tree

  #include using namespace std;  int ans = 0;  vector edges[100000];  void Add_edge(int u, int v){    edges[u].push_back(v);    edges[v].push_back(u);}  void minTimeToColor(int node, int parent,                    int arrival_time){            int current_time = 0;      for (auto x : edges[node]) {                  if (x != parent) {                                      ++current_time;                                      if (current_time == arrival_time)                ++current_time;                          ans = max(ans, current_time);                                      minTimeToColor(x, node, current_time);        }    }}  int main(){      pair A[] = { { 1, 2 },                            { 2, 3 },                            { 3, 4 } };      for (auto i : […]

Continue Reading

Largest element in an N-ary Tree

Largest element in an N-ary TreeGiven an N-ary tree consisting of N nodes, the task is to find the node having the largest value in the given N-ary Tree.Examples:Input: Output: 90Explanation: The node with the largest value in the tree is 90.Input: Output: 95Explanation: The node with the largest value in the tree is 95.Approach: […]

Continue Reading

Count of subtrees possible from an N-ary Tree

   #include #define MAX 300004 using namespace std;    vector graph[MAX]; int mod = 1e9 + 7;    int ans = 0;    int countSubtreesUtil(int cur, int par) {               int res = 1;             for (int i = 0;          i < graph[cur].size(); i++) {                     int v = graph[cur][i];            if […]

Continue Reading