二叉树转换为链表
2017-09-12 10:37
267 查看
一、问题描述
输入一棵二叉搜索树,现在要将该二叉搜索树转换成一个排序的双向链表。而且在转换的过程中,不能创建任何新的结点,只能调整树中的结点指针的指向来实现。
查阅了相关内容,基本了解了这个问题的解析方法与思想。
二叉搜索树转换为有序双向链表
搜索二叉树转换成链表
二叉搜索树与双向链表
输入一棵二叉搜索树,现在要将该二叉搜索树转换成一个排序的双向链表。而且在转换的过程中,不能创建任何新的结点,只能调整树中的结点指针的指向来实现。
查阅了相关内容,基本了解了这个问题的解析方法与思想。
二叉搜索树转换为有序双向链表
DList BSTreeToList(BSTree tree) { if(tree == NULL) return NULL; //找到最左边的结点,即转换后链表的头结点 DLNode *head = FindLeftmostNode(tree); BSNode *last_node = NULL; //进行转换 ConvertNode(tree, &last_node); return head; } BSNode* FindLeftmostNode(BSTree tree) { if(tree == NULL) return NULL; while(tree->left != NULL) tree = tree->left; return tree; } void ConvertNode(BSTree tree, BSNode **last_node) { if(tree == NULL) return; //对tree的左子树进行转换,last_node是转换后链表最后一个结点的指针 if(tree->left != NULL) ConvertNode(tree->left, last_node); //调整tree的left指针,指向上一个结点 tree->left = *last_node; //调整指向最后一个结点,right指向下一个结点 if(*last_node != NULL) (*last_node)->right = tree; //调整指向最后链表一个结点的指针 *last_node = tree; //对tree的右子树进行转换,last_node是转换后链表最后一个结点的指针 if(tree->right != NULL) ConvertNode(tree->right, last_node); }
搜索二叉树转换成链表
//搜索二叉树->双链表 void BinaryTreeToDoublyList(BiTree &root) { if (root == NULL) return; if (root->lChild == NULL && root->rChild == NULL) return; BiNode* left = root->lChild; BiNode* right = root->rChild; BinaryTreeToDoublyList(left); BinaryTreeToDoublyList(right); //找到左子树的最大结点 if(left) { while(left->rChild) { left = left->rChild; } left->rChild = root; root->lChild = left; } if (right) { //找到右子树的最大结点 while(right->lChild) { right = right->lChild; } right->lChild = root; root->rChild = right; } } void BinaryTreeToDoublyListMain(BiTree &root) { if (root == NULL) return; BinaryTreeToDoublyList(root); //root指向双向链表头 if (root->lChild) root = root->lChild; }
二叉搜索树与双向链表
void Convert(BinaryTreeNode* pNode,BinaryTreeNode** pLastNodeInList) { if(pNode==NULL) return; BinaryTreeNode* pCurrent=pNode; //左子树转换,遍历到左子树的叶子结点 if(pCurrent->m_pLeft!=NULL)//遍历左子树 Convert(pCurrent->m_pLeft,pLastNodeInList); pCurrent->m_pLeft=*pLastNodeInList; if((*pLastNodeInList)!=NULL) (*pLastNodeInList)->m_pRight=pCurrent; *pLastNodeInList=pCurrent; //右子树转换 if(pCurrent->m_pRight!=NULL)//遍历右子树 Convert(pCurrent->m_pRight,pLastNodeInList); } BinaryTreeNode* Convert(BinaryTreeNode* pRoot) { BinaryTreeNode* pLastNodeInList=NULL;//指向双向链表的尾结点 Convert(pRoot,&pLastNodeInList);//转换排序二叉树为双向链表 //求双向链表的头结点 BinaryTreeNode* pHeadOfList=pLastNodeInList; while(pHeadOfList!=NULL&&pHeadOfList->m_pLeft!=NULL) pHeadOfList=pHeadOfList->m_pLeft; return pHeadOfList; }
相关文章推荐
- 把二叉树转换成双向链表
- 把二叉树转换为双向链表
- 二叉树转换为双向链表,以及二叉树相关操作---纪念考研的时光
- 面试100题系列之1将查找二叉树转换成有序的双向链表
- LeetCode OJ 之 Flatten Binary Tree to Linked List (把二叉树转换成链表)
- 把二叉树转换成双向链表
- 将二叉树的叶子结点转换成单链表,并返回最左叶子结点的地址(链头)
- 将二叉树转换成链表
- 二叉树转换为双向环形链表
- BST(搜索二叉树)与双向链表相互转换
- 将搜索二叉树转换成双向链表
- 面试题之剑指offer 二叉树转换成双向链表
- 【学习点滴-数据结构-二叉树】二叉查找树转换成双链表
- 将搜索二叉树转换成双向链表
- 将二叉树结构转换为双向链表 java语言实现
- 二叉树--将二叉搜索树转换成一个排序的双向链表
- IT公司100题-1-二叉树转换为双链表
- 数据结构 算法面试100题 之 二叉树转换成双向链表
- 将一棵二叉树转换为双向链表的俩中算法
- 递归将二叉树转换为双向链表