二叉搜索树转换成双向链表
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<map>
using namespace std;
struct node
{
int val;
node * left,*right;
node(int _val):val(_val),left(NULL),right(NULL){}
};
void convertnode(node* root,node *& lastlistnode)
{
if(root==NULL) return;
if(root->left)
convertnode(root->left,lastlistnode);
root->left=lastlistnode;
if(lastlistnode) lastlistnode->right=root;
lastlistnode=root;
if(root->right)
convertnode(root->right,lastlistnode);
}
node* convert(node * root)
{
node* plistnode=NULL;
convertnode(root,plistnode);
while(plistnode->left)
plistnode=plistnode->left;
return plistnode;
}
int main()
{
node n1(10),n2(6),n3(14),n4(4),n5(8),n6(12),n7(16);
n1.left=&n2;
n1.right=&n3;
n2.left=&n4;
n2.right=&n5;
n3.left=&n6;
n3.right=&n7;
node * ans=convert(&n1);
while (ans)
{
cout<<ans->val<<" ";
ans=ans->right;
}
return 0;
}
//" abbaabba "