Convert a binary tree into a two-way linked list

Source: Internet
Author: User

I remember this is a Microsoft interview question. I thought about it for a long time and did not know how to do it. I recently read other people's blogs and found an algorithm. I implemented it myself. The following describes the algorithm:

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 root in left subtree (inorder predecessor is 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 are similar to left subtree ).
..... 2. a) Recursively convert the right subtree to DLL.
..... 2. B) Then find inorder successor of root in right subtree (inorder successor is 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 converted DLL ).

 

 

The following code is provided:


[Cpp]
# Include <iostream>
Using namespace std;
 
Typedef struct tree_node_s {
Int data;
Struct tree_node_s * lchild;
Struct tree_node_s * rchild;
} Tree_node_t;
 
 
Tree_node_t * changeToListUtil (tree_node_t * root ){
If (NULL = root)
Return NULL;
If (root-> lchild ){
Tree_node_t * left = changeToListUtil (root-> lchild );
While (left-> rchild ){
Left = left-> rchild;
}
Root-> lchild = left;
Left-> rchild = root;
}
If (root-> rchild ){
Tree_node_t * right = changeToListUtil (root-> rchild );
While (right-> lchild ){
Right = right-> lchild;
}
Root-> rchild = right;
Right-> lchild = root;
}
Return root;
}
 
Tree_node_t * changeToList (tree_node_t * root ){
If (NULL = root)
Return NULL;
Root = changeToListUtil (root );
While (root-> lchild)
Root = root-> lchild;
Return root;
}
 
 
Void printList (tree_node_t * head ){
While (head ){
Cout Head = head-> rchild;
}
}
 
Tree_node_t * createNode (int data ){
Tree_node_t * temp = (tree_node_t *) malloc (sizeof (tree_node_t ));
Temp-> data = data;
Temp-> lchild = NULL;
Temp-> rchild = NULL;
Return temp;
}
 
Int main (int argc, char * argv []) {
Tree_node_t * root = createNode (10 );
Root-> lchild = createNode (12 );
Root-> rchild = createNode (15 );
Root-> lchild = createNode (25 );
Root-> lchild-> rchild = createNode (30 );
Root-> rchild-> lchild = createNode (36 );
 
Tree_node_t * head = changeToList (root );
PrintList (head );
Cin. get ();
Return 0;
}

# Include <iostream>
Using namespace std;

Typedef struct tree_node_s {
Int data;
Struct tree_node_s * lchild;
Struct tree_node_s * rchild;
} Tree_node_t;


Tree_node_t * changeToListUtil (tree_node_t * root ){
If (NULL = root)
Return NULL;
If (root-> lchild ){
Tree_node_t * left = changeToListUtil (root-> lchild );
While (left-> rchild ){
Left = left-> rchild;
}
Root-> lchild = left;
Left-> rchild = root;
}
If (root-> rchild ){
Tree_node_t * right = changeToListUtil (root-> rchild );
While (right-> lchild ){
Right = right-> lchild;
}
Root-> rchild = right;
Right-> lchild = root;
}
Return root;
}

Tree_node_t * changeToList (tree_node_t * root ){
If (NULL = root)
Return NULL;
Root = changeToListUtil (root );
While (root-> lchild)
Root = root-> lchild;
Return root;
}


Void printList (tree_node_t * head ){
While (head ){
Cout Head = head-> rchild;
}
}

Tree_node_t * createNode (int data ){
Tree_node_t * temp = (tree_node_t *) malloc (sizeof (tree_node_t ));
Temp-> data = data;
Temp-> lchild = NULL;
Temp-> rchild = NULL;
Return temp;
}

Int main (int argc, char * argv []) {
Tree_node_t * root = createNode (10 );
Root-> lchild = createNode (12 );
Root-> rchild = createNode (15 );
Root-> lchild = createNode (25 );
Root-> lchild-> rchild = createNode (30 );
Root-> rchild-> lchild = createNode (36 );

Tree_node_t * head = changeToList (root );
PrintList (head );
Cin. get ();
Return 0;
}

 

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.