Inorder Tree Traversal without Recursion 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 = current->right c) Go to step 3. 5) If current is NULL and stack is empty then we are done.
Read full article from Inorder Tree Traversal without Recursion | GeeksforGeeks
No comments:
Post a Comment