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

Spread the post

Print all paths in a binary tree which sum up to a value

Print all path which sum up to a value in binary tree using stack.

For example Given binary tree are contain following nodes.

1030201010-104010205020

Output

View Code execution process.

Stack Areamainroot(pointer)top(pointer) NULL Heap Areastruct Treedata (int)= 10left (pointer) right (pointer) struct Treedata (int)= 30left (pointer) right (pointer) struct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= -10left (pointer) right (pointer) struct Treedata (int)= 40left (pointer) right (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 10left (pointer) right (pointer)= NULLstruct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 50left (pointer)= NULLright (pointer) struct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULL Stack Areamainroot(pointer)top(pointer)printk_sumauxilary (int) =0k (int) =50max_value (int) =0store(pointer)temp(pointer)top(pointer)pushnew_node(pointer)node(pointer)top(pointer) Heap Areastruct Treedata (int)= 10left (pointer) right (pointer) struct Treedata (int)= 30left (pointer) right (pointer) struct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= -10left (pointer) right (pointer) struct Treedata (int)= 40left (pointer) right (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 10left (pointer) right (pointer)= NULLstruct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 50left (pointer)= NULLright (pointer) struct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULLstruct Stackvisit (int)= 2link (pointer) next (pointer)= NULLstruct Stackvisit (int)= 1link (pointer) next (pointer) struct Stackvisit (int)= 2link (pointer) next (pointer) struct Stackvisit (int)= ? link (pointer)= ? next (pointer)= ?

Try it Yourself

C program for Print all ancestor of given node key in binary tree.


Output

Spread the post

Recommended Posts: