微软面试题
2015-07-30 11:42
330 查看
这道题是微软的面试题,大意是给出一个二叉排序树,让你输出排序的链表
二叉排序树参照博客http://blog.csdn.net/hackbuteer1/article/details/7275396
转换部分参照博客http://zhedahht.blog.163.com/
#include <iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<stack>
using namespace std;
struct node
{
int num;
struct node *lc;
struct node *rc;
};
bool searchtree(node *root ,int data,node *f,node *&p)
{
if(root==NULL)
{
p=f;
return false;
}
else if(root->num==data)
{
p=root;
return true;
}
else if(root->num>data)
{
return searchtree(root->lc,data,root,p);
}
else if(root->num<data)
{
return searchtree(root->rc,data,root,p);
}
}
void buildtree(node *&root,int data)
{
node *p;
node *q;
if(!searchtree(root,data,NULL,p))
{
q=(struct node *)malloc(sizeof(node));
q->num=data;
q->lc=NULL;
q->rc=NULL;
if(p==NULL)
{
root=q;
}
else if(data<p->num)p->lc=q;
else p->rc=q;
}
else return;
}
void convertnode(node *p,node *&lastoflist)
{
if(p==NULL)return;
node *pcurrent=p;
if(pcurrent->lc)
{
convertnode(pcurrent->lc,lastoflist);
}
pcurrent->lc=lastoflist;
if(lastoflist)
lastoflist->rc=pcurrent;
lastoflist=pcurrent;
if(pcurrent->rc)
{
convertnode(p->rc,lastoflist);
}
}
node *zhuanhuan(node *p)
{
node*lastoflist=NULL;
convertnode(p,lastoflist);
node*headoflist=lastoflist;
while(headoflist&&headoflist->lc)
{
headoflist=headoflist->lc;
}
return headoflist;
}
void zhongxubianli(node *p)
{
if(p==NULL)return;
zhongxubianli(p->lc);
printf("%d ",p->num);
zhongxubianli(p->rc);
}
int main()
{
node *ptree=NULL;
int n;
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
int x;
scanf("%d",&x);
buildtree(ptree,x);
}
node*phead=zhuanhuan(ptree);
while(phead)
{
printf("%d ",phead->num);
phead=phead->rc;
}
return 0;
}
二叉排序树参照博客http://blog.csdn.net/hackbuteer1/article/details/7275396
转换部分参照博客http://zhedahht.blog.163.com/
#include <iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<stack>
using namespace std;
struct node
{
int num;
struct node *lc;
struct node *rc;
};
bool searchtree(node *root ,int data,node *f,node *&p)
{
if(root==NULL)
{
p=f;
return false;
}
else if(root->num==data)
{
p=root;
return true;
}
else if(root->num>data)
{
return searchtree(root->lc,data,root,p);
}
else if(root->num<data)
{
return searchtree(root->rc,data,root,p);
}
}
void buildtree(node *&root,int data)
{
node *p;
node *q;
if(!searchtree(root,data,NULL,p))
{
q=(struct node *)malloc(sizeof(node));
q->num=data;
q->lc=NULL;
q->rc=NULL;
if(p==NULL)
{
root=q;
}
else if(data<p->num)p->lc=q;
else p->rc=q;
}
else return;
}
void convertnode(node *p,node *&lastoflist)
{
if(p==NULL)return;
node *pcurrent=p;
if(pcurrent->lc)
{
convertnode(pcurrent->lc,lastoflist);
}
pcurrent->lc=lastoflist;
if(lastoflist)
lastoflist->rc=pcurrent;
lastoflist=pcurrent;
if(pcurrent->rc)
{
convertnode(p->rc,lastoflist);
}
}
node *zhuanhuan(node *p)
{
node*lastoflist=NULL;
convertnode(p,lastoflist);
node*headoflist=lastoflist;
while(headoflist&&headoflist->lc)
{
headoflist=headoflist->lc;
}
return headoflist;
}
void zhongxubianli(node *p)
{
if(p==NULL)return;
zhongxubianli(p->lc);
printf("%d ",p->num);
zhongxubianli(p->rc);
}
int main()
{
node *ptree=NULL;
int n;
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
int x;
scanf("%d",&x);
buildtree(ptree,x);
}
node*phead=zhuanhuan(ptree);
while(phead)
{
printf("%d ",phead->num);
phead=phead->rc;
}
return 0;
}
相关文章推荐
- 黑马程序员--反射
- 程序员Web开发手册
- 【剑指Offer面试题】 九度OJ1516:调整数组顺序使奇数位于偶数前面
- 程序员必备技能
- 程序员如何爱护自己的眼睛
- 面试
- 扣丁学堂O2O在线课
- 面试题36:数组中的逆序对
- 毕业后就是程序员——我的阿里、金山、中华、腾讯、360、网易面试总结(三)
- 毕业后就是程序员——我的阿里、金山、中华、腾讯、360、网易面试总结(二)
- 毕业后就是程序员——我的阿里、金山、中华、腾讯、360、网易面试总结(一)
- 读书笔记——《程序员自我修养》——内存不够怎么办?
- android面试遇到的一些经典题
- 剑指Offer面试题10(Java版):二进制中的1的个数
- 黑马程序员----反射-获取字节码文件对象三种方式
- 自学android半年,已从.net转型成android程序员,分享下这个过程
- 【LeetCode-面试算法经典-Java实现】【077-Combinations(组合数)】
- 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】
- 【LeetCode-面试算法经典-Java实现】【074-Search a 2D Matrix(搜索二维矩阵)】
- 黑马程序员 日记(六)