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

Spread the post

Merge two binary trees by doing node sum

Write an efficient algorithm to merge two given binary tree.

Suppose following nodes are insert in binary trees.

Output

Before Merge

Stack Areamainroot(pointer)root1(pointer) 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)= NULLstruct Treedata (int)= 5left (pointer) right (pointer) struct Treedata (int)= 1left (pointer) right (pointer)= NULLstruct Treedata (int)= 1left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 2left (pointer)= NULLright (pointer)= NULL

After Merger

Stack Areamainroot(pointer)root1(pointer) Heap Areastruct Treedata (int)= 15left (pointer) right (pointer) struct Treedata (int)= 3left (pointer) right (pointer)= NULLstruct Treedata (int)= 10left (pointer) right (pointer) struct Treedata (int)= 7left (pointer) right (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer) struct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer) struct Treedata (int)= 1left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 1left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 2left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULL

Program for merger given two binary tree. Time complexity O(n).

Output

View process.

Stack Areamainroot(pointer)root1(pointer)mergerroot1(pointer)root2(pointer)mergerroot1(pointer)root2(pointer)mergerroot1(pointer)root2(pointer)mergerroot1 (pointer) ? root2 (pointer) ? Heap Areastruct Treedata (int)= 15left (pointer) right (pointer) struct Treedata (int)= 3left (pointer) right (pointer)= NULLstruct Treedata (int)= 10left (pointer) right (pointer) struct Treedata (int)= 7left (pointer) right (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer) struct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer) struct Treedata (int)= 1left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 1left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 2left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULL

View steps of execution

Submit your solution in comment section.

Spread the post

Recommended Posts: