Given a ternary tree, create a doubly linked list out of it. A ternary tree is just like binary tree but instead of having two nodes, it has three nodes i.e. left, middle, right.
The doubly linked list should holds following properties –
- Left pointer of ternary tree should act as prev pointer of doubly linked list.
- Middle pointer of ternary tree should not point to anything.
- Right pointer of ternary tree should act as next pointer of doubly linked list.
- Each node of ternary tree is inserted into doubly linked list before its subtrees and for any node, its left child will be inserted first, followed by mid and right child (if any).
For the above example, the linked list formed for below tree should be NULL 5 1 4 8 11 6 7 15 63 31 55 65 -> NULL
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to traverse the tree in preoder fashion similar to binary tree preorder traversal. Here, when we visit a node, we will insert it into doubly linked list in the end using a tail pointer. That we use to maintain the required insertion order. We then recursively call for left child, middle child and right child in that order.
Below is the implementation of this idea.
Created Double Linked list is: 30 5 1 4 8 11 6 7 15 63 31 55 65
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- Construct a Doubly linked linked list from 2D Matrix
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Create new linked list from two given linked list with greater element at each node
- Create a linked list from two linked lists by choosing max element at each position
- Memory efficient doubly linked list
- Delete a node in a Doubly Linked List
- QuickSort on Doubly Linked List
- Merge Sort for Doubly Linked List
- Check if a doubly linked list of characters is palindrome or not
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Doubly Circular Linked List | Set 2 (Deletion)