Using Stack is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See this for step wise step execution of the algorithm.

1) Create an empty stack S.
2) Initialize current node as root
3) Push the current node to S and set current = current->left until current is NULL
4) If current is NULL and stack is not empty then 
     a) Pop the top item from stack.
     b) Print the popped item, set current = popped_item->right 
     c) Go to step 3.
5) If current is NULL and stack is empty then we are done.

Let us consider the below tree for example

            1
          /   \
        2      3
      /  \
    4     5

Step 1 Creates an empty stack: S = NULL

Step 2 sets current as address of root: current -> 1

Step 3 Pushes the current node and set current = current->left until current is NULL
     current -> 1
     push 1: Stack S -> 1
     current -> 2
     push 2: Stack S -> 2, 1
     current -> 4
     push 4: Stack S -> 4, 2, 1
     current = NULL

Step 4 pops from S
     a) Pop 4: Stack S -> 2, 1
     b) print "4"
     c) current = NULL /*right of 4 */ and go to step 3
Since current is NULL step 3 doesn't do anything. 

Step 4 pops again.
     a) Pop 2: Stack S -> 1
     b) print "2"
     c) current -> 5/*right of 2 */ and go to step 3

Step 3 pushes 5 to stack and makes current NULL
     Stack S -> 5, 1
     current = NULL

Step 4 pops from S
     a) Pop 5: Stack S -> 1
     b) print "5"
     c) current = NULL /*right of 5 */ and go to step 3
Since current is NULL step 3 doesn't do anything

Step 4 pops again.
     a) Pop 1: Stack S -> NULL
     b) print "1"
     c) current -> 3 /*right of 5 */  

Step 3 pushes 3 to stack and makes current NULL
     Stack S -> 3
     current = NULL

Step 4 pops from S
     a) Pop 3: Stack S -> NULL
     b) print "3"
     c) current = NULL /*right of 3 */
[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%23define%20bool%20int%0A%20%0A%2F*%20A%20binary%20tree%20tNode%20has%20data%2C%20pointer%20to%20left%20child%0A%20%20%20and%20a%20pointer%20to%20right%20child%20*%2F%0Astruct%20tNode%0A%7B%0A%20%20%20int%20data%3B%0A%20%20%20struct%20tNode*%20left%3B%0A%20%20%20struct%20tNode*%20right%3B%0A%7D%3B%0A%20%0A%2F*%20Structure%20of%20a%20stack%20node.%20Linked%20List%20implementation%20is%20used%20for%20%0A%20%20%20stack.%20A%20stack%20node%20contains%20a%20pointer%20to%20tree%20node%20and%20a%20pointer%20to%20%0A%20%20%20next%20stack%20node%20*%2F%0Astruct%20sNode%0A%7B%0A%20%20struct%20tNode%20*t%3B%0A%20%20struct%20sNode%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Stack%20related%20functions%20*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20struct%20tNode%20*t)%3B%0Astruct%20tNode%20*pop(struct%20sNode**%20top_ref)%3B%0Abool%20isEmpty(struct%20sNode%20*top)%3B%0A%20%0A%2F*%20Iterative%20function%20for%20inorder%20tree%20traversal%20*%2F%0Avoid%20inOrder(struct%20tNode%20*root)%0A%7B%0A%20%20%2F*%20set%20current%20to%20root%20of%20binary%20tree%20*%2F%0A%20%20struct%20tNode%20*current%20%3D%20root%3B%0A%20%20struct%20sNode%20*s%20%3D%20NULL%3B%20%20%2F*%20Initialize%20stack%20s%20*%2F%0A%20%20bool%20done%20%3D%200%3B%0A%20%0A%20%20while%20(!done)%0A%20%20%7B%0A%20%20%20%20%2F*%20Reach%20the%20left%20most%20tNode%20of%20the%20current%20tNode%20*%2F%0A%20%20%20%20if(current%20!%3D%20%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%2F*%20place%20pointer%20to%20a%20tree%20node%20on%20the%20stack%20before%20traversing%20%0A%20%20%20%20%20%20%20%20the%20node’s%20left%20subtree%20*%2F%0A%20%20%20%20%20%20push(%26s%2C%20current)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20current%20%3D%20current-%3Eleft%3B%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%20backtrack%20from%20the%20empty%20subtree%20and%20visit%20the%20tNode%20%0A%20%20%20%20%20%20%20at%20the%20top%20of%20the%20stack%3B%20however%2C%20if%20the%20stack%20is%20empty%2C%0A%20%20%20%20%20%20you%20are%20done%20*%2F%0A%20%20%20%20else%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20if%20(!isEmpty(s))%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20current%20%3D%20pop(%26s)%3B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20current-%3Edata)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20we%20have%20visited%20the%20node%20and%20its%20left%20subtree.%0A%20%20%20%20%20%20%20%20%20%20Now%2C%20it’s%20right%20subtree’s%20turn%20*%2F%0A%20%20%20%20%20%20%20%20current%20%3D%20current-%3Eright%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20done%20%3D%201%3B%20%0A%20%20%20%20%7D%0A%20%20%7D%20%2F*%20end%20of%20while%20*%2F%20%0A%7D%20%20%20%20%20%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Function%20to%20push%20an%20item%20to%20sNode*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20struct%20tNode%20*t)%0A%7B%0A%20%20%2F*%20allocate%20tNode%20*%2F%0A%20%20struct%20sNode*%20new_tNode%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20sNode*)%20malloc(sizeof(struct%20sNode))%3B%0A%20%0A%20%20if(new_tNode%20%3D%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20%20printf(%22Stack%20Overflow%20%5Cn%22)%3B%0A%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20exit(0)%3B%0A%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%0A%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20new_tNode-%3Et%20%20%3D%20t%3B%0A%20%0A%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20tNode%20*%2F%0A%20%20new_tNode-%3Enext%20%3D%20(*top_ref)%3B%20%20%20%0A%20%0A%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20tNode%20*%2F%0A%20%20(*top_ref)%20%20%20%20%3D%20new_tNode%3B%0A%7D%0A%20%0A%2F*%20The%20function%20returns%20true%20if%20stack%20is%20empty%2C%20otherwise%20false%20*%2F%0Abool%20isEmpty(struct%20sNode%20*top)%0A%7B%0A%20%20%20return%20(top%20%3D%3D%20NULL)%3F%201%20%3A%200%3B%0A%7D%20%20%20%0A%20%0A%2F*%20Function%20to%20pop%20an%20item%20from%20stack*%2F%0Astruct%20tNode%20*pop(struct%20sNode**%20top_ref)%0A%7B%0A%20%20struct%20tNode%20*res%3B%0A%20%20struct%20sNode%20*top%3B%0A%20%0A%20%20%2F*If%20sNode%20is%20empty%20then%20error%20*%2F%0A%20%20if(isEmpty(*top_ref))%0A%20%20%7B%0A%20%20%20%20%20printf(%22Stack%20Underflow%20%5Cn%22)%3B%0A%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20exit(0)%3B%0A%20%20%7D%0A%20%20else%0A%20%20%7B%0A%20%20%20%20%20top%20%3D%20*top_ref%3B%0A%20%20%20%20%20res%20%3D%20top-%3Et%3B%0A%20%20%20%20%20*top_ref%20%3D%20top-%3Enext%3B%0A%20%20%20%20%20free(top)%3B%0A%20%20%20%20%20return%20res%3B%0A%20%20%7D%0A%7D%0A%20%0A%2F*%20Helper%20function%20that%20allocates%20a%20new%20tNode%20with%20the%0A%20%20%20given%20data%20and%20NULL%20left%20and%20right%20pointers.%20*%2F%0Astruct%20tNode*%20newtNode(int%20data)%0A%7B%0A%20%20struct%20tNode*%20tNode%20%3D%20(struct%20tNode*)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20malloc(sizeof(struct%20tNode))%3B%0A%20%20tNode-%3Edata%20%3D%20data%3B%0A%20%20tNode-%3Eleft%20%3D%20NULL%3B%0A%20%20tNode-%3Eright%20%3D%20NULL%3B%0A%20%0A%20%20return(tNode)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%0A%7B%0A%20%0A%20%20%2F*%20Constructed%20binary%20tree%20is%0A%20%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20%20%20%2F%20%20%20%5C%0A%20%20%20%20%20%20%20%202%20%20%20%20%20%203%0A%20%20%20%20%20%20%2F%20%20%5C%0A%20%20%20%204%20%20%20%20%205%0A%20%20*%2F%0A%20%20struct%20tNode%20*root%20%3D%20newtNode(1)%3B%0A%20%20root-%3Eleft%20%20%20%20%20%20%20%20%3D%20newtNode(2)%3B%0A%20%20root-%3Eright%20%20%20%20%20%20%20%3D%20newtNode(3)%3B%0A%20%20root-%3Eleft-%3Eleft%20%20%3D%20newtNode(4)%3B%0A%20%20root-%3Eleft-%3Eright%20%3D%20newtNode(5)%3B%20%0A%20%0A%20%20inOrder(root)%3B%0A%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Time Complexity: O(n)

Output:

 4 2 5 1 3

Categorized in: