# Replace each node in binary tree with the sum of its inorder predecessor and successor

Given a binary tree containing n nodes. The problem is to replace each node in the binary tree with the sum of its inorder predecessor and inorder successor.

Examples:

```Input :          1
/   \
2     3
/  \  /  \
4   5  6   7

Output :        11
/    \
9      13
/ \    /  \
2   3   4   3

For 1:
Inorder predecessor = 5
Inorder successor  = 6
Sum = 11

For 4:
Inorder predecessor = 0
(as inorder predecessor is not present)
Inorder successor  = 2
Sum = 2

For 7:
Inorder predecessor = 3
Inorder successor  = 0
(as inorder successor is not present)
Sum = 3
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Create an array arr. Store 0 at index 0. Now, store the inorder traversal of tree in the array arr. Then, store 0 at last index. 0’s are stored as inorder predecessor of leftmost leaf and inorder successor of rightmost leaf is not present. Now, perform inorder traversal and while traversing node replace node’s value with arr[i-1] + arr[i+1] and then increment i. In the beginning initialize i = 1. For an element arr[i], the values arr[i-1] and arr[i+1] are its inorder predecessor and inorder successor respectively.

```// C++ implementation to replace each node
// in binary tree with the sum of its inorder
// predecessor and successor
#include <bits/stdc++.h>

using namespace std;

// node of a binary tree
struct Node {
int data;
struct Node* left, *right;
};

// function to get a new node of a binary tree
struct Node* getNode(int data)
{
// allocate node
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));

// put in the data;
new_node->data = data;
new_node->left = new_node->right = NULL;

return new_node;
}

// function to store the inorder traversal
// of the binary tree in 'arr'
void storeInorderTraversal(struct Node* root,
vector<int>& arr)
{
// if root is NULL
if (!root)
return;

// first recur on left child
storeInorderTraversal(root->left, arr);

// then store the root's data in 'arr'
arr.push_back(root->data);

// now recur on right child
storeInorderTraversal(root->right, arr);
}

// function to replace each node with the sum of its
// inorder predecessor and successor
void replaceNodeWithSum(struct Node* root,
vector<int> arr, int* i)
{
// if root is NULL
if (!root)
return;

// first recur on left child
replaceNodeWithSum(root->left, arr, i);

// replace node's data with the sum of its
// inorder predecessor and successor
root->data = arr[*i - 1] + arr[*i + 1];

// move 'i' to point to the next 'arr' element
++*i;

// now recur on right child
replaceNodeWithSum(root->right, arr, i);
}

// Utility function to replace each node in binary
// tree with the sum of its inorder predecessor
// and successor
void replaceNodeWithSumUtil(struct Node* root)
{
// if tree is empty
if (!root)
return;

vector<int> arr;

// store the value of inorder predecessor
// for the leftmost leaf
arr.push_back(0);

// store the inoder traversal of the tree in 'arr'
storeInorderTraversal(root, arr);

// store the value of inorder successor
// for the rightmost leaf
arr.push_back(0);

// replace each node with the required sum
int i = 1;
replaceNodeWithSum(root, arr, &i);
}

// function to print the preorder traversal
// of a binary tree
void preorderTraversal(struct Node* root)
{
// if root is NULL
if (!root)
return;

// first print the data of node
cout << root->data << " ";

// then recur on left subtree
preorderTraversal(root->left);

// now recur on right subtree
preorderTraversal(root->right);
}

// Driver program to test above
int main()
{
// binary tree formation
struct Node* root = getNode(1); /*         1        */
root->left = getNode(2);        /*       /   \      */
root->right = getNode(3);       /*     2      3     */
root->left->left = getNode(4);  /*    /  \  /   \   */
root->left->right = getNode(5); /*   4   5  6   7   */
root->right->left = getNode(6);
root->right->right = getNode(7);

cout << "Preorder Traversal before tree modification:n";
preorderTraversal(root);

replaceNodeWithSumUtil(root);

cout << "\nPreorder Traversal after tree modification:n";
preorderTraversal(root);

return 0;
}
```

Output:

```Preorder Traversal before tree modification:
1 2 4 5 3 6 7
Preorder Traversal after tree modification:
11 9 2 3 13 4 3
```

Time Complexity: O(n)
Auxiliary Space: O(n)

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3 Average Difficulty : 3/5.0
Based on 1 vote(s)