• 二元查找树转换成一个排序的双向链表


    题目:
    输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
    要求不能创建任何新的结点,只调整指针的指向。
       10
      / /
      6  14
     / / / /
    4  8 12 16
     转换成双向链表

    4=6=8=10=12=14=16。

    #ifndef UTILS_HEADER
    #define UTILS_HEADER
    #include "utils.h"
    #endif
    #include "MS.h"


    struct treenode* MS01Util(struct treenode* root, int flag){

        if (root == NULL){
            return NULL;
        }
        struct treenode* left = MS01Util(root->lchild, 0);
        struct treenode* right = MS01Util(root->rchild, 1);
        if (left != NULL){
            left->rchild = root;
            root->lchild = left;
        }
        if (right != NULL){
            right->lchild = root;
            root->rchild = right;
        }
        struct treenode* result = root;
        if (flag == 0){
            while (root != NULL){
                result = root;
                root = root->rchild;
            }
        }
        else{
            while (root != NULL){
                result = root;
                root = root->lchild;
            }
        }
        return result;
    }
    void MS01(){

        double array[] = { 23154263512463214 };
        struct treenode* root = gene2OrTree(array, 17);
        cout << endl;
        root = MS01Util(root, 1);
        while (root != NULL){
            cout << root->value << " ";
            root = root->rchild;
        }
        cout << endl;


        return;

    } 

  • 相关阅读:
    看MySQL官方文档的示例SQL有感
    MySQL 主外键约束与标准SQL不同的地方
    pip安装包时遇到的Bug
    MySQL update 语句与标准SQL不同的地方
    让MySQL在美国标准下运行
    mysqlslap 压力测试工具
    django 自动化测试的故障排查
    mysql-8.0.11 比较坑的地方dba门要淡定
    python3 如何给装饰器传递参数
    python3 装饰器应用举例
  • 原文地址:https://www.cnblogs.com/jilichuan/p/3758019.html
Copyright © 2020-2023  润新知