[Geeksforgeeks] Convert a given Binary Tree to doubly Linked List

Source: Internet
Author: User

http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/

Given a Binary Tree (Bt), convert it to a doubly Linked List (DLL). The left and right pointers in nodes is to is used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must is same as inorder of the given Binary Tree. The first node of inorder traversal (left most node in BT) must is the head node of the DLL.

I came across this interview during one of my interviews. A similar problem is discussed in this post. The problem is simpler as we don't need to create circular DLLs, but a simple DLL. the idea behind its solution are quite simple and straight.

1. If left subtree exists, process the left subtree
.....1.a)Recursively convert the left subtree to DLL.
.....1.b)  Then find inorder predecessor of the root in the left subtree (inorder predecessor was rightmost node in left subtree).
.....1.c)  Make Inorder predecessor as previous of Root and root as next of Inorder predecessor.
2. If right subtree exists, process the right subtree (Below 3 steps is similar to left subtree).
.....2.a)  Recursively convert the right subtree to DLL.
.....2.b)Then find Inorder successor of the root in right subtree (inorder successor are leftmost node in right subtree).
..... 2.c) Make Inorder successor as next of root and root as previous of Inorder successor.
3. Find the leftmost node and return it (the leftmost node is always head of the converted DLL).

Below is the source code for above algorithm.

//A C + + program for in-place conversion of Binary Tree to DLL#include <stdio.h>/*A Binary tree node has data, and left and right pointers*/structnode{intdata; Node*Left ; Node*Right ;};/*The the core function to convert a Tree to list. This function Followssteps 1 and 2 of the above algorithm*/node* Bintree2listutil (node*root) {    //Base Case    if(Root = =NULL)returnRoot; //Convert The left subtree and link to root    if(Root->left! =NULL) {        //Convert the left subtreeNode* left = Bintree2listutil (root->Left ); //Find inorder predecessor. After the This loop, the left//Would point to the inorder predecessor         for(; left->right!=null; left=left->Right ); //Make root as next of the predecessorLeft->right =Root; //Make predecssor as previous of rootRoot->left =Left ; }    //Convert the right subtree and link to root    if(root->right!=NULL) {        //Convert the right subtreenode* right = Bintree2listutil (root->Right ); //Find inorder successor. After the This loop, right//Would point to the inorder successor         for(; right->left!=null; right = right->Left ); //Make Root as previous of successorRight->left =Root; //Make successor as next of RootRoot->right =Right ; }    returnRoot;}//The main function that first calls Bintree2listutil () and then follows step 3//Of the above algorithmnode* Bintree2list (Node *root) {    //Base Case    if(Root = =NULL)returnRoot; //Convert to DLL using Bintree2listutil ()Root =Bintree2listutil (root); //Bintree2listutil () returns root node of the converted//DLL. We need pointer to the leftmost node which are//Head of the constructed DLL, so move to the leftmost node     while(Root->left! =NULL) Root= root->Left ; return(root);}/*Helper function that is allocates a new node with Thegiven data and NULL left and right pointers.*/node* NewNode (intdata) {Node* New_node =Newnode; New_node->data =data; New_node->left = New_node->right =NULL; return(New_node);}/*Function to print nodes in a given doubly linked list*/voidPrintlist (Node *node) {     while(node!=NULL) {printf ("%d", node->data); Node= node->Right ; }}/*Driver program to test above functions*/intMain () {//Let us create the tree shown in above diagramNode *root = NewNode (Ten); Root->left = NewNode ( A); Root->right = NewNode ( the); Root->left->left = NewNode ( -); Root->left->right = NewNode ( -); Root->right->left = NewNode ( $); //Convert to DLLNode *head =bintree2list (root); //Print the converted listPrintlist (head); return 0;}

[Geeksforgeeks] Convert a given Binary Tree to doubly Linked List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.