
- 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
Flatten Binary Tree to Linked List in C++
Suppose we have a binary tree; we have to flatten it into linked list in place. So if the tree is like −
The output tree will be −
To solve this, we will follow these steps −
ser prev := null
Define a recursive function solve(), that will take root as input.
if root is null, then return
solve(right of root)
solve(left of root)
right of root := prev, left of root := null
prev := root
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; }else{ q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; }else{ q.push(temp->right); } } } TreeNode *make_tree(vector<int> v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } class Solution { public: TreeNode* prev = NULL; void flatten(TreeNode* root) { if(!root) return; flatten(root->right); flatten(root->left); root->right = prev; root->left = NULL; prev = root; } }; main(){ vector<int> v = {1,2,5,3,4}; TreeNode *root = make_tree(v); Solution ob; (ob.flatten(root)); TreeNode *ptr = root; while(ptr != NULL && ptr->val != 0){ cout << ptr->val << ", "; ptr = ptr->right; } }
Input
[1,2,5,3,4]
Output
1, 2, 3, 4, 5,
- Related Articles
- Linked List in Binary Tree in C++
- Python Program to Implement Binary Tree using Linked List
- Program to create linked list to binary search tree in Python
- Flatten a multilevel 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
- Program to convert binary search tree to a singly linked list in C++?
- Python program to convert a given binary tree to doubly linked list
- Program to convert a linked list into a binary search tree in C++
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Program to find out if a linked list is present in a given binary tree in Python
- Convert Sorted List to Binary Search Tree in C++
- Binary Search on Singly Linked List in C++
- C++ Program to Implement a Binary Search Tree using Linked Lists

Advertisements