Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Replace binary tree nodes by sum of predecessor and successor

Write an efficient algorithm to Replace each binary tree nodes by sum of predecessor successor.

Suppose following nodes are insert in binary trees.

Suppose given binary tree is.

Stack Areamainauxiliary (int) ? predecessor(pointer) NULLroot(pointer)successor(pointer) NULL Heap Areastruct Treedata (int)= 10left (pointer) right (pointer) struct Treedata (int)= 2left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer) right (pointer) struct Treedata (int)= 7left (pointer) right (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULL

Program for Replace the BT nodes of sum of predecessor and successor. Time complexity O(n).

Output

View process.

Stack Areamainauxiliary (int) =2predecessor(pointer)root(pointer)successor(pointer)replacehelper(pointer)predecessor(pointer)root(pointer)successor(pointer)replacehelper(pointer)predecessor(pointer)root(pointer)successor(pointer) Heap Areastruct Treedata (int)= 10left (pointer) right (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer) right (pointer) struct Treedata (int)= 7left (pointer) right (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULL

View steps of execution

Submit your solution in comment section.

Spread the post

Recommended Posts: