Wednesday, October 26, 2022
HomeWordPress DevelopmentDiscover dad and mom which can be Okay degree above given node...

Discover dad and mom which can be Okay degree above given node for Q queries


  

#embody <bits/stdc++.h>

utilizing namespace std;

  

struct Node {

    int val;

    Node *left, *proper;

    Node(int x)

    {

        val = x;

        left = proper = NULL;

    }

};

  

void dfs(Node* cur, Node* father or mother, vector<vector<int> >& par)

{

    if (cur == NULL)

        return;

    par[0][cur->val] = parent->val;

    dfs(cur->left, cur, par);

    dfs(cur->proper, cur, par);

}

  

int findKlevelup(int u, int okay, vector<vector<int> >& par)

{

    for (int i = 0; i < 19; i++) {

        if ((okay >> i) & 1)

            u = par[i][u];

    }

    return (u ? u : -1);

}

  

void clear up(int n, int q, vector<vector<int> >& queries,

           Node* root)

{

    vector<vector<int> > par(20, vector<int>(n, 0));

    par[0][root->val] = 0;

    dfs(root->left, root, par);

    dfs(root->proper, root, par);

  

    

    for (int i = 1; i <= 18; i++) {

        for (int j = 1; j <= n; j++)

            par[i][j] = par[i - 1][par[i - 1][j]];

    }

  

    

    for (int i = 0; i < q; i++) {

        int u, okay;

        u = queries[i][0];

        okay = queries[i][1];

  

        

        

        int ans = findKlevelup(u, okay, par);

        cout << ans << " ";

    }

}

  

int essential()

{

    int N = 5;

    int Q = 3;

  

    

    Node* root = new Node(1);

    root->left = new Node(2);

    root->proper = new Node(3);

    root->right->left = new Node(4);

    root->right->proper = new Node(5);

  

    vector<vector<int> > queries;

    queries = { { 4, 1 }, { 4, 2 }, { 4, 3 } };

  

    

    clear up(N, Q, queries, root);

  

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments