January 10, 2025 |26.2K Views

    Iterative Preorder Traversal

      Share  2 Likes
    Description
    Discussion

    Given a Binary Tree, write an iterative function to print the Preorder traversal of the given binary tree.
    Refer to this for recursive preorder traversal of Binary Tree. To convert an inherently recursive procedure to iterative, we need an explicit stack. Following is a simple stack based iterative process to print Preorder traversal. 
    1) Create an empty stack nodeStack and push root node to stack. 
    2) Do the following while nodeStack is not empty. 
    ….a) Pop an item from the stack and print it. 
    ….b) Push right child of a popped item to stack 
    ….c) Push left child of a popped item to stack

    Iterative Preorder Traversal : https://www.geeksforgeeks.org/iterative-preorder-traversal/