Given an n-ary tree, print preorder traversal of it.
Preorder traversal of below tree is A B K N M J F D G E C H I L
The idea is to use stack like iterative preorder traversal of binary tree.
1) Create an empty stack to store nodes.
2) Push the root node to the stack.
3) Run a loop while the stack is not empty
….a) Pop the top node from stack.
….b) Print the popped node.
….c) Store all the children of popped node onto the stack. We must store children from right to left so that leftmost node is popped first.
4) If stack is empty then we are done.
A B K N M J F D G E C O H I L
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Iterative Preorder Traversal of an N-ary Tree
- Construct a special tree from given preorder traversal
- Construct the full k-ary tree from its preorder traversal
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Modify a binary tree to get preorder traversal using right pointers only
- Find n-th node in Preorder traversal of a Binary Tree
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- DFS traversal of a tree using recursion
- Zig-Zag traversal of a Binary Tree using Recursion
- Inorder Tree Traversal without Recursion
- Inorder Tree Traversal without recursion and without stack!
- Postorder traversal of Binary Tree without recursion and without stack
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Find postorder traversal of BST from preorder traversal
- Construct BST from given preorder traversal | Set 2
- Iterative Preorder Traversal
- Morris traversal for Preorder
- Print Postorder traversal from given Inorder and Preorder traversals
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.