【发布时间】:2020-09-08 18:43:44
【问题描述】:
我正在尝试将 二叉树转换为单链表。我正在尝试解决 O(n) 时间复杂度的问题。实现 O( n) 时间复杂度我返回链接列表的头部和尾部。我得到了错误的输出,请帮助我。
方法 1 工作正常。为了降低方法 1 的时间复杂度。我需要删除用于遍历左链接列表的 while 循环。因此我需要返回链接的 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;
}
【问题讨论】:
-
如果您提供一个可运行的minimal reproducible example 并显示错误的输出,这将有所帮助。如果您要求我将您的程序完成为可编译的程序,然后尝试找出它对哪些输入做了一些违反您期望的事情,老实说,我宁愿做其他事情......
-
好的,我已经上传了完整的可运行代码。
标签: algorithm data-structures linked-list c++14 binary-search-tree