看到一个大神关于面试的文章
http://blog.renren.com/blog/258531212/942892717
里面提到了这个问题(好吧,大神只是用这个例子的原因是,大神觉得这个问题简单爆了)
看到有人提到这个问题还可以升级为不利用辅助空间来实现。想了半天之后发现,在不增加时间开销的情况下,把辅助空间降为O(1)是无解的。
(也可能是我太笨,所以想不到,如果你想到了,请留言给我)
和其他人算法一样,我的代码也是通过递归。核心代码算是有10句吧。
void to_double_link_list(Node* root,Node*&head,Node*&last_node)
{
head=root;
last_node=root;
if(root!=NULL)
{
Node *temp1;
Node *temp2;
if(root->left_!=NULL)
{
to_double_link_list(root->left_,head,temp1);
temp1->right_=root;
root->left_=temp1;
}
if(root->right_!=NULL)
{
to_double_link_list(root->right_,temp2,last_node);
temp2->left_=root;
root->right_=temp2;
}
}
}
后记:后来又看了一下那个日志,大神后记中增加了一个改进版,和这个思路是相同的。最喜欢看大神之间打架了
完整二叉树代码
#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <queue>
class Node
{
public:
Node(int key):key_(key),left_(NULL),right_(NULL){}
~Node();
void Insert(int key);
void Delete(int key);
void Print();
void Print2(int x=64);
friend void to_double_link_list(Node*root,Node*&head,Node*&last_node);
friend void PrintList(Node*head);
friend void PrintListR(Node*last);
private:
int key_;
Node* left_;
Node* right_;
};
void to_double_link_list(Node* root,Node*&head,Node*&last_node)
{
head=root;
last_node=root;
if(root!=NULL)
{
Node *temp1;
Node *temp2;
if(root->left_!=NULL)
{
to_double_link_list(root->left_,head,temp1);
temp1->right_=root;
root->left_=temp1;
}
if(root->right_!=NULL)
{
to_double_link_list(root->right_,temp2,last_node);
temp2->left_=root;
root->right_=temp2;
}
}
}
void PrintList(Node*head)
{
while(head!=NULL)
{
std::cout<<":"<<head->key_;
head=head->right_;
}
std::cout<<std::endl;
}
void PrintListR(Node*head)
{
while(head!=NULL)
{
std::cout<<":"<<head->key_;
head=head->left_;
}
std::cout<<std::endl;
}
Node::~Node()
{
if(left_!=NULL)
{
delete left_;
left_=NULL;
}
if(right_!=NULL)
{
delete right_;
right_=NULL;
}
};
//typedef struct t_note node;
void Node::Insert(int key)
{
if(key <= key_)
{
if (left_==NULL)
{
Node*new_node=new Node(key);
left_=new_node;
}
else
{
left_->Insert(key);
}
}
else if(key>key_)
{
if (right_==NULL)
{
Node*new_node=new Node(key);
right_=new_node;
}
else
{
right_->Insert(key);
}
}
}
void Node::Delete(int key)
{
}
void Node::Print()
{
if(left_!=NULL)
{
left_->Print();
}
std::cout<<":"<<key_;
if(right_!=NULL)
{
right_->Print();
}
}
void Node::Print2(int x)
{
std::queue<Node*> node_queue;
node_queue.push(this);
int last_layer=false;
int layer=1;
while(last_layer==false)
{
int l= node_queue.size();
last_layer=true;
int space_count=x/(1<<layer)-1;
for(int i=0;i<l;i++)
{
Node*temp=node_queue.front();
node_queue.pop();
for(int j=0;j<space_count;j++)
{
std::cout<<" ";
}
if(i!=0)
{
for(int j=0;j<space_count;j++)
{
std::cout<<" ";
}
}
if(temp==NULL)
{
std::cout<<" ";
node_queue.push(NULL);
node_queue.push(NULL);
continue;
}
printf("%2d",temp->key_);
if(temp->key_!=NULL || temp->right_!=NULL)
{
last_layer=false;
}
node_queue.push(temp->left_);
node_queue.push(temp->right_);
}
std::cout<<std::endl;
layer++;
}
}
int main()
{
int r=rand()%100;//没有设随机种子,要的就是这种效果
Node *root=new Node(r);
for(int i=0;i<9;i++)
{
r= rand()%100;
root->Insert(r);
}
root->Print();
std::cout<<std::endl;
root->Print2();
Node*head;
Node*last_node;
to_double_link_list(root,head,last_node);//转换后调用析构函数时会出错,可以改变析构函数,或者手动释放内存,我懒得改了
PrintList(head);
PrintListR(last_node);
system("pause");
//delete root;
return 0;
}