
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
In this tutorial, we will be discussing a program to convert a binary tree to a doubly linked list.
For this we will be provided with a binary tree. Our task is to convert it into a doubly linked list such that the left and right pointers become the previous and next pointers. Also the sequential order of the doubly linked list must be equal to the inorder traversal of the binary tree.
For this we are having a very straight forward approach. We will be traversing the binary tree in in order way making the nodes of the doubly linked list along with and finally making the left and right to be the previous and next nodes respectively.
Example
#include <iostream> using namespace std; //node structure of binary tree struct node{ int data; node* left; node* right; }; //traversing and making nodes for //doubly linked list void binarytodll(node *root, node **head){ if (root == NULL) return; static node* prev = NULL; //converting left subtree binarytodll(root->left, head); if (prev == NULL) *head = root; else { root->left = prev; prev->right = root; } prev = root; //converting right subtree binarytodll(root->right, head); } //allocating a new node node* newNode(int data) { node* new_node = new node; new_node->data = data; new_node->left = new_node->right = NULL; return (new_node); } //printing nodes of doubly linked list void print_dll(node *node){ while (node!=NULL) { cout << node->data << " "; node = node->right; } } int main(){ node *root = newNode(10); root->left = newNode(12); root->right = newNode(15); root->left->left = newNode(25); root->left->right = newNode(30); root->right->left = newNode(36); node *head = NULL; binarytodll(root, &head); print_dll(head); return 0; }
Output
25 12 30 10 36 15
- Related Articles
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Python program to convert a given binary tree to doubly linked list
- Convert a Binary Tree to a Circular Doubly Link List in C++
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Program to convert a linked list into a binary search tree in C++
- Program to convert binary search tree to a singly linked list in C++?
- Program to convert linked list to zig-zag binary tree in Python
- Program to convert level order binary tree traversal to linked list in Python
- Convert an Array to a Circular Doubly Linked List in C++
- Python program to create a doubly linked list from a ternary tree
- Flatten Binary Tree to Linked List in C++
- Linked List in Binary Tree in C++
- Program to find out if a linked list is present in a given binary tree in Python
- Delete a Doubly Linked List node at a given position in C++
- Python Program to Implement Binary Tree using Linked List

Advertisements