输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。例如把下述二叉查找树
10
/ /
6 14
/ / / /
4 8 12
转换成双向链表,即得:
4=6=8=10=12=14=16。
void tree2Dll(TNode* root, TNode*& tail) {
if (!root) {
return;
}
if (root->left) {
tree2Dll(root->left, tail);
}
TNode* tmp = root;
tmp->left = tail;
if (tail) {
tail->right = tmp;
}
tail = tmp;
if (root->right) {
tree2Dll(root->right, tail);
}
}