微软面试题001-二叉树转化为链表
2011-05-17 15:38
218 查看
#include<iostream> #include<stdlib.h> using namespace std; struct BSTreeNode{ BSTreeNode *pleft; BSTreeNode *pright; int numvaule; }; typedef BSTreeNode DoubleList; DoubleList *phead; DoubleList *pListIndex; void convert(BSTreeNode *pCurrent); void addBSTreeNode(BSTreeNode *& pcurrent, int value) { if ( pcurrent == NULL) { BSTreeNode *pBSTree = new BSTreeNode(); pBSTree->pleft = NULL; pBSTree->pright = NULL; pBSTree->numvaule = value; pcurrent = pBSTree; }else { if((pcurrent->numvaule )> value) { addBSTreeNode( pcurrent->pleft, value); } else if((pcurrent->numvaule )< value) { addBSTreeNode( pcurrent->pright, value); }else{ cout<<"invaild input"<<endl; } } } int ergodic(BSTreeNode *pcurrent) { if (NULL == pcurrent){ cout<<"The BS tree is empty"<<endl; return -1; } if (NULL != pcurrent->pleft) { ergodic(pcurrent->pleft); } convert(pcurrent); if(NULL != pcurrent->pright) { ergodic(pcurrent->pright); } return 0; } void convert(BSTreeNode * pcurrent) { pcurrent->pleft = pListIndex; if (NULL != pListIndex){ pListIndex->pright = pcurrent; }else{ phead = pcurrent; } pListIndex = pcurrent; } void printList(DoubleList *phead) { DoubleList *ptr = phead; do{ cout<<ptr->numvaule<<endl; ptr = ptr->pright; }while( NULL != ptr); } int main(int argc, char **argv) { BSTreeNode *proot = NULL; addBSTreeNode(proot, 10); addBSTreeNode(proot, 8); addBSTreeNode(proot, 9); addBSTreeNode(proot, 4); addBSTreeNode(proot, 16); addBSTreeNode(proot, 17); addBSTreeNode(proot, 12); addBSTreeNode(proot, 11); addBSTreeNode(proot, 23); ergodic(proot); printList(phead); system("pause"); return 0; }
相关文章推荐
- 面试题怎么将二叉树转化为链表
- 微软面试题:树转化为双链表
- 微软面试百题001——BST转化有序双向链表
- 二叉树经典面试题2~将搜索二叉树转化为双向链表
- 微软等名企面试题4--二叉树专题
- 将搜索二叉树转化为双向链表
- 【微软面试题01】二元查找树转换为双向链表(未完成)
- 微软面试题 单向链表找环
- 微软系列面试题c/c++第一题双向链表
- 36. 微软面试题:请修改append函数, 利用函数实现(链表)
- 面试题:搜索二叉树转双向链表
- 层次遍历二叉树 ——微软面试题
- 微软面试题:写程序找出二叉树的深度
- 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
- 微软面试题: 找出二叉树上任意两个结点的最近共同父结点。
- 【BST】把搜索二叉树转化成双链表
- C/C++ 微软面试题 -链表
- 【2013微软面试题】输出节点数为n的二叉树的所有形态
- 微软面试题解题笔记——二元查找树转排序双向链表
- 微软面试题之两个链表的第一个公共结点