二叉树转换成双向链表(day6) 发表于 2016-11-20 | 题目 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。比如将二元查找树 12345678将 10 / \ 6 14 / \ / \ 4 8 12 16 转换成双向链表4=6=8=10=12=14=16 实现1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253struct Node { int value; Node* left; Node* right;};bool createList(Node* head, Node* &left, Node* &right){ left = head; right = head; if(!head){ return false; } else { Node *l1, *r1, *l2, *r2; l1 = NULL; r1 = NULL; l2 = NULL; r2 = NULL; if(createList(head->left, l1, r1)){ head->left = r1; r1->right = head; left = l1; } if(createList(head->right, l2, r2)){ head->right=l2; l2->left=head; right = r2; } return true; }}int main(){ Node n4={4,NULL,NULL}; Node n5={8,NULL,NULL}; Node n6={12,NULL,NULL}; Node n7={16, NULL, NULL}; Node n2={6,&n4,&n5}; Node n3={14,&n6,&n7}; Node n1={10,&n2,&n3}; Node*Left=NULL; Node*Right=NULL; createList(&n1,Left,Right); while(Left){ cout<<Left->value<<endl; Left=Left->right; } return 0;} 源码github 赏 微信打赏 支付宝打赏