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

Spread the post

Find the sum of all cousins of a given key in a binary tree

Given a binary tree node and find the sum of all cousins this particular nodes.

For example Given binary tree are contain following nodes.

12475836910

Output

View cousin of nodes 10.

12475836910

Node 10 are cousins represent are blue circle. and the sum of cousins node are [8+7]=15.

Another example.

12475836910

Node 5 are cousin are 6.

View Code execution process.

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULL Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer) right (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer) right (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULL

Try it Yourself

C program for Sum of all cousins of node that is given as input.


Output

view process

Stack Areamainhead(pointer)root(pointer)tail(pointer)find_cousinauxiliary(pointer)level (int) =0node (int) =5result (int) =0status (int) =0temp(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer) right (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer) right (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Queuelevel (int)= 0parent (pointer)= NULLlink (pointer) next (pointer) struct Queuelevel (int)= 1parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 1parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 2parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 2parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 2parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 3parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 3parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 3parent (pointer) link (pointer) next (pointer) struct Queuelevel (int)= 3parent (pointer) link (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: