OFFER prove safety of binary search trees and doubly-linked list (nine degrees OJ1503)

Subject description:

Enter a binary search tree, the binary search tree converted into a doubly linked list sorted. Requirements can not create any new node, point to only adjust the tree node pointer.

 

Input:

Input may contain multiple test samples.
For each test case, a first input of a number of behaviors n (0 <n <1000) , is representative of the number of test samples.
The next n lines, each of the behavior of a binary search tree sequence preorder traversal, wherein when the left and right subtrees substitutes 0 is empty.

 

Output:

Corresponding to each test case,
the output of the binary search tree converted into a doubly linked list sorted from the head of the list to the end of the linked list traversal results.

 

Sample input:
1
2 1 0 0 3 0 0

 

Sample output:
1 2 3

Problem-solving ideas:

  This question should be the most tedious recently wrote a question of.

  Before the first input by the rules, the need to preorder traversal input

void createTree(BTree **b){  
    int m;
    scanf("%d",&m);
    if(m == 0)
        *b = NULL;
    else{
        BTree *s = (BTree *)malloc(sizeof(BTree));
        s->data = m;
        s->lchild = NULL;
        s->rchild = NULL;
        *b = s;
        createTree(&((*b)->lchild));
        createTree(&((*b)->rchild));
    }
}

  Further, the whole idea, is a double-stranded conversion table entry pointer record tail.

  Each traversal sequence is converted.

  The first conversion is the most lower left node,

void converNode(BTree *b,BTree **p){
    if(b == NULL)
        return ;
    BTree *pnow = b;
    if(b->lchild != NULL)
        converNode(b->lchild,p);
    
    pnow->lchild = *p;
    if(*p != NULL)
        (*p)->rchild = pnow;

    *p = pnow;

    if(b->rchild != NULL)
        converNode(b->rchild,p);
}

  Every child traverse left and right child nodes. The left child node point to the end of the list of conversion and the end of the right child pointers point to themselves. Right child pointing to the right child node. Without the right to return the child. The following step is the code ideas:

1 is left to find the smallest node, PLast = NULL;

 

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

 

Finally, we traverse the pointer returned back from the tail.

All Code:

#include <stdio.h>
#include <stdlib.h>
typedef struct btree{
    int data;
    struct btree *lchild,*rchild;
}BTree;
 
void createTree(BTree **b);
void inorderTree(BTree *b);
BTree * convert(BTree *b);
void converNode(BTree *b,BTree **p);
 
int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        BTree *b = (BTree *)malloc(sizeof(BTree));   
        createTree(&b);
        BTree *head = convert(b);
        while(head!=NULL){
            printf("%d ",head->data);
            head = head->rchild;
        }
        printf("\n");
    }
    return 0;
}
BTree* convert(BTree *b){
    BTree *pLast = NULL;
    converNode(b,&pLast);
 
    BTree *phead = pLast;
    while(phead != NULL && phead->lchild != NULL)
        phead = phead->lchild;
    return phead;
}
void converNode(BTree *b,BTree **p){
    if(b == NULL)
        return ;
    BTree *pnow = b;
    if(b->lchild != NULL)
        converNode(b->lchild,p);
     
    pnow->lchild = *p;
    if(*p != NULL)
        (*p)->rchild = pnow;
 
    *p = pnow;
 
    if(b->rchild != NULL)
        converNode(b->rchild,p);
}
void createTree(BTree **b){  
    int m;
    scanf("%d",&m);
    if(m == 0)
        *b = NULL;
    else{
        BTree *s = (BTree *)malloc(sizeof(BTree));
        s->data = m;
        s->lchild = NULL;
        s->rchild = NULL;
        *b = s;
        createTree(&((*b)->lchild));
        createTree(&((*b)->rchild));
    }
}
/**************************************************************
    Problem: 1503
    User: xhalo
    Language: C
    Result: Accepted
    Time:80 ms
    Memory:1704 kb
****************************************************************/

 

Reproduced in: https: //my.oschina.net/u/204616/blog/544960

Guess you like

Origin blog.csdn.net/weixin_33798152/article/details/91989926
Recommended