Sword refers to the OFFER Binary Search Tree and two-way linked list (9du OJ1503)

Source: Internet
Author: User

Description:

Enter a binary search tree and convert it into a sorted two-way linked list. It is required that no new node can be created, and only the point of the node pointer in the tree can be adjusted.

 

Input:

The input may contain multiple test examples.
For each test case, the number of input first act is n (0 <n <1000), which indicates the number of test samples.
For the next n rows, each behavior is a binary search tree's first sequential traversal sequence. If left and right subtree is empty, it is replaced by 0.

 

Output:

Corresponding to each test case,
Output the result of converting the binary search tree into a sorted two-way linked list and traversing from the linked list header to the end of the linked list.

 

Sample input:
12 1 0 0 3 0 0

 

Sample output:
1 2 3
Solution:

This question should be the most tedious one recently written.

First, enter rules. You need to traverse the input in the forward order.

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));    }}

In addition, the overall idea is to use a pointer to record the end of a converted double-chain table.

Converts the traversal in the middle order each time.

The first node to be converted is the leftmost 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);}

  The left child and right child of each traversal node. Point the left child to the End Node of the conversion linked list, and point the right child of the end pointer to himself. The right child points to the right child of the node. If no right child exists, return. The following are the steps of the Code:

1. Find the leftmost node, that is, the smallest node. PLast = NULL;

 

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

 

Finally, we traverse the back pointer from the end and return the result.

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****************************************************************/

 

Related Article

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.