【题目】:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。
比如将二元查找树 。
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
【代码】:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
///////////////////////////////////////////////////////////////////////
// Covert a sub binary-search-tree into a sorted double-linked list // Input: pNode - the head of the sub tree // pLastNodeInList - the tail of the double-linked list /////////////////////////////////////////////////////////////////////// void ConvertNode(BSTreeNode *pNode, BSTreeNode *&pLastNodeInList) { if(pNode == NULL) return; BSTreeNode *pCurrent = pNode; // Convert the left sub-tree // Put the current node into the double-linked list // Update the last node in list // Convert the right sub-tree /////////////////////////////////////////////////////////////////////// // Get the head of the double-linked list return pHeadOfList; |
【参考】:
http://blog.****.net/yysdsyl/article/details/1841632
http://www.cnblogs.com/wolenski/archive/2012/07/08/2581859.html