Norway


// structure to store a node

struct Node

{

    int data;

    Node *left, *right, *next;

};

 

// Function to next of all in a binary tree

// curr -> current node

// prev -> previous processed node (Passed by reference)

void setNextNode(Node* curr, Node* &prev)

{

    // return if tree is empty

    if (curr == nullptr)

        return;

    // recurse for left subtree

    setNextNode(curr->left, prev);

    // set previous node next pointer to current node

    if (prev != nullptr)

        prev->next = curr;

    // update previous node to current node

    prev = curr;

    // recurse for right subtree

    setNextNode(curr->right, prev);

}

// Function to print of all nodes of

// binary tree using next pointer

void inorderSuccessor(Node* curr)

{

    Node* prev = nullptr;

    // set next pointer of all nodes

    setNextNode(curr, prev);

    // go to leftmost node

    curr = curr->left->left;

    // print inorder Successor of all nodes

    while (curr->next)

    {

        cout << “Inorder Successor of node “ << curr->data << ” is “

            << curr->next->data << endl;

        curr = curr->next;

    }

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here