将二叉树转换为单链接列表

问题描述

我正在尝试将二叉树转换为单链列表。我正在尝试解决 O(n)时间复杂度的问题。要实现O( n)时间复杂度我要同时返回链接列表的开头和结尾。我收到了错误输出,请帮帮我。

方法1正常工作。为减少方法1的时间复杂性,我需要删除用于遍历左侧链接列表的while循环。因此,我需要返回Link的Head和Tail指针列表。
这两种方法本质上都是递归的。

/**************** To Construct LL from a BST ***************/

#include <iostream>
using namespace std;
#include "BinaryTreeNode.h"
#include "linklist_class.cpp"
#include <queue>

void printTreeLevelWise(BinaryTreeNode<int>* root){
    if(root == NULL){       //Base case
        return;
    }
    queue<BinaryTreeNode<int>*> pendingNodes;
    pendingNodes.push(root);
    while(pendingNodes.size() != 0){
        BinaryTreeNode<int>* front = pendingNodes.front();
        pendingNodes.pop();
        cout<<front->data<<":";
        if(front->left != NULL){
            cout<<"L"<<front->left->data;
            pendingNodes.push(front->left);
        }
        if(front->right != NULL){
            cout<<"R"<<front->right->data;
            pendingNodes.push(front->right);
        }
        cout<<endl;
    }
    
}

BinaryTreeNode<int>* takeInputLevelWise(){
    int rootData;
    cout<<"Enter root data"<<endl;
    cin>>rootData;
    if(rootData == -1){
        return NULL;
    }
    BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);
    queue<BinaryTreeNode<int>*> pendingNodes;
    pendingNodes.push(root);
    
    while(pendingNodes.size() != 0){
        BinaryTreeNode<int>* front = pendingNodes.front();
        pendingNodes.pop();
        
        cout<<"Enter Left Child of "<<front->data<<endl;
        int leftChildData;
        cin>>leftChildData;
        if(leftChildData != -1){
                BinaryTreeNode<int>* child = new BinaryTreeNode<int>(leftChildData);
                front->left = child;
                pendingNodes.push(child);
        }
        
        cout<<"Enter right Child of "<<front->data<<endl;
        int rightChildData;
        cin>>rightChildData;
        if(rightChildData != -1){
                BinaryTreeNode<int>* child = new BinaryTreeNode<int>(rightChildData);
                front->right = child;
                pendingNodes.push(child);
        }
        
    }
    return root; 
}
/******* To print Link list  *********/
void print(node *head){            
    while(head != NULL){         
        cout<<head ->data<<" ";
        head = head->next;
    }
    cout<<endl;
}

/***************************** Approach1 ******************************/
/* Working Correctly */

node* LLfromBST(BinaryTreeNode<int>* root){
    if(root == NULL){
        return NULL;
    }
    node* lhead = LLfromBST(root->left);
    node* rhead = LLfromBST(root->right);
    node* newNode = new node(root->data);
    
    
    newNode->next = rhead;
    
    // lhead head is NULL Then we will try to access NULL->next
    // to stop this let's put a check
    if(!lhead){           
        return newNode;
    }
    
    node* temp = lhead ;    
    while(temp->next != NULL){
        temp = temp->next;
    } 
    temp->next = newNode;
    
    return lhead;
}


/***************************** Approach2 ******************************/
/* Not Working */
//O(n)  //head = p.first and tail = p.second

pair<node*,node*> LLfromBST2(BinaryTreeNode<int>* root){
    if(root == NULL){
        pair<node*,node*> p ;
        p.first = NULL;
        p.second = NULL;
        return p;
    }
    pair<node*,node*> lp  = LLfromBST2(root->left);
    pair<node*,node*> rp  = LLfromBST2(root->right);
    
    node* newNode = new node(root->data);
    newNode->next = rp.first;   
    
    // lhead head is NULL Then we will try to access NULL->next
    // to stop this let's put a check
    if(!lp.first){        
        pair <node*,node*> p;
        p.first = newNode;
        p.second = rp.second;
        return p;
    }
    lp.second->next = newNode;
    pair <node*,node*> p;
    p.first = lp.first;
    p.second = rp.second;
    return p;
}


int main(){
    // BST :   4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
    
    BinaryTreeNode<int>* root = takeInputLevelWise();
    printTreeLevelWise(root);
    cout<<endl;
    //Approach 1
    /*node* head = LLfromBST(root);
    print(head);
    cout<<endl;*/
    //Approach 2
    node* head2 = LLfromBST2(root).first;
    print(head2);
    delete root;
    return 0;   
}

解决方法

仅按顺序遍历BST将产生节点的排序列表。因此,您需要如下所示的内容:

        void buildLLFromBST(BinaryTreeNode<int>* root,ListNode<int>* node) {
            if (root) {
               buildLLFromBST(root->left,node);
               node->next = root;
               node = node->next;
               buildLLFromBST(root->right,node);
            }
        }

,您可以这样称呼它:buildLLFromBST(root,dummynode)