Binary search tree and doubly linked list

Source: Internet
Author: User

Title Description Enter a binary search tree and convert the two-fork search tree into a sorted doubly linked list. Requires that no new nodes can be created, only the point pointer of the node in the tree can be adjusted.
1 /*2 struct TreeNode {3 int val;4 struct TreeNode *left;5 struct TreeNode *right;6 TreeNode (int x):7 val (x), left (null), right (null) {8     }9 };*/Ten classSolution { One  Public: Atreenode* Convert (treenode*prootoftree) -     { -         if(Prootoftree==null)returnNULL; thetreenode* head=Prootoftree; -treenode* tail=NULL; -Converttree (head,&tail); -          while(head->left!=NULL) +Head=head->Left ; -         returnhead; +          A     } at Private: -     voidConverttree (treenode* root,treenode**tail) { -         if(root==NULL) -             return; -treenode* tmp=Root; -         if(root->left!=NULL) { inConverttree (root->left,tail); -         } toroot->left=*tail; +         if((*tail)! =NULL) -(*tail)->right=tmp; the*tail=tmp; *         if(tmp->right!=NULL) $Converttree (tmp->right,tail);Panax Notoginseng     } -};

Transferred from: http://www.cnblogs.com/xing901022/p/3781713.html

Title Description:

Enter a binary search tree and convert the two-fork search tree into a sorted doubly linked list. Requires that no new nodes can be created, only the point pointer of the node in the tree can be adjusted.

Input:

The input may contain multiple test samples.
For each test case, the first behavior of the input is a number n (0<n<1000), which represents the number of test samples.
The next n rows, each of which behaves as a binary search tree, first sequence traversal sequence, in which the left and right subtree is empty is replaced by 0.

Output:

corresponding to each test case,
The output traverses the two-fork search tree into a sorted doubly linked list, from the chain header to the end of the chain.

Sample input:
12 1 0 0 3 0 0

Sample output:
1 2 3
1 voidCreatetree (BTree * *b) {  2     intm;3scanf"%d",&m);4     if(M = =0)5(bb=NULL;6     Else{7BTree *s = (BTree *)malloc(sizeof(BTree));8S->data =m;9S->lchild =NULL;TenS->rchild =NULL; One*b =s; ACreatetree (& (*b)lchild)); -Createtree (& (*b)rchild)); -     } the}

In addition, the overall idea is to record the converted double-linked list footer with a pointer.

Each time the conversion of the middle order traversal is performed.

The first to convert is the bottom left node,

1 voidConvernode (BTree *b,btree * *p) {2     if(b = =NULL)3         return ;4BTree *pnow =b;5     if(B->lchild! =NULL)6Convernode (b->lchild,p);7     8Pnow->lchild = *p;9     if(*p! =NULL)Ten(*p)->rchild =Pnow; One  A*p =Pnow; -  -     if(B->rchild! =NULL) theConvernode (b->rchild,p); -}

Each time you traverse a node, the left child is the right child. Point the left child to the tail node of the transition list and point the right child at the end pointer to itself. Right child points to the right child of the node. If there is no right child, return. Here are the steps for code thinking:

1 Find the leftmost and smallest node, PLast = NULL;

2 Let the left child of the node point to the end of the chain, then the chain tail pointer moves right. If the right child is empty, return.

Finally we traverse back from the tail to return the pointer.

Go back to the top of all the code:
1#include <stdio.h>2#include <stdlib.h>3typedefstructbtree{4     intdata;5     structBtree *lchild,*Rchild;6 }btree;7  8 voidCreatetree (BTree * *b);9 voidInordertree (BTree *b);TenBTree * CONVERT (BTree *b); One voidConvernode (BTree *b,btree * *p); A   - intMain () { -     intN; thescanf"%d",&n); -      while(n--){ -BTree *b = (BTree *)malloc(sizeof(BTree));  -Createtree (&b); +BTree *head =convert (b); -          while(head!=NULL) { +printf"%d",head->data); AHead = head->Rchild; at         } -printf"\ n"); -     } -     return 0; - } -btree* CONVERT (BTree *b) { inBTree *plast =NULL; -Convernode (b,&pLast); to   +BTree *phead =PLast; -      while(Phead! = NULL && Phead->lchild! =NULL) thePhead = phead->Lchild; *     returnPhead; $ }Panax Notoginseng voidConvernode (BTree *b,btree * *p) { -     if(b = =NULL) the         return ; +BTree *pnow =b; A     if(B->lchild! =NULL) theConvernode (b->lchild,p); +       -Pnow->lchild = *p; $     if(*p! =NULL) $(*p)->rchild =Pnow; -   -*p =Pnow; the   -     if(B->rchild! =NULL)WuyiConvernode (b->rchild,p); the } - voidCreatetree (BTree * *b) {   Wu     intm; -scanf"%d",&m); About     if(M = =0) $*b =NULL; -     Else{ -BTree *s = (BTree *)malloc(sizeof(BTree)); -S->data =m; AS->lchild =NULL; +S->rchild =NULL; the*b =s; -Createtree (& (*b)lchild)); $Createtree (& (*b)rchild)); the     } the } the /************************************************************** the problem:1503 - User:xhalo in Language:c the result:accepted the time:80 Ms About memory:1704 KB the ****************************************************************/

Problem Solving Ideas:

This question should be the most tedious one that has been written recently.

First input by rule, need to carry forward sequence traversal input

Binary search tree and 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.