Spread the post

Max sum path from root to leaf in a binary tree

Max sum path from root to leaf in a binary tree. Solve this problem in bottom up approach using stack.

For example Given Binary tree are contain following nodes.

9187653

Max sum path is : 29

View pointers and nodes of tree.

Stack Areamainroot(pointer)top (pointer) ? inorder_showtemp(pointer)inorder_showtemp(pointer) Heap Areastruct Treedata (int)= 9left (pointer) right (pointer) struct Treedata (int)= 1left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer) right (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULL

View max sum path in a tree

9187653

Try it Yourself

C program for Sum of nodes on the longest path from root to leaf node.


Output

view process

Stack Areamainroot(pointer)top(pointer)max_sum_pathauxilary (int) =29max_value (int) =29store(pointer)temp(pointer)top(pointer)pushnew_node(pointer)node(pointer)top(pointer) Heap Areastruct Treedata (int)= 9left (pointer) right (pointer) struct Treedata (int)= 1left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer) right (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULLstruct Stackvisit (int)= 2link (pointer) next (pointer)= NULLstruct Stackvisit (int)= 2link (pointer) next (pointer) struct Stackvisit (int)= 2link (pointer) next (pointer) struct Stackvisit (int)= 0link (pointer) next (pointer)= ?

Try it Yourself

Spread the post

Recommended Posts: