Write an efficient algorithm to sum of all existing node in given binary tree.
solving of this problem using recursion are very easy. but here given a iterative solution using stack.
Suppose following nodes are inserted on binary tree.
Iterative solution : This given below function accept two parameter.
a) root node of tree and
b) address of stack pointer
Preorder traversal of tree in iterative manner and add this node value.
View nodes and pointers.
Try it Yourself
Program for Sum of existing nodes in binary tree. iterative solution using stack.
Visualize process Try it Yourself
Exercise Solve this problem using inorder and postorder traversal.