阿里2015年校园招聘笔试题
2014-09-05 21:16
302 查看
题目:写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函数返回这颗二叉树中相差最大的两个节点间的差值绝对值。请注意程序效率。
说明:根据题目的要求首先需要构造一个二叉树,首先想到的是构造一个二叉搜索树,这样找就可以很方便的寻找到最大值和最小值,从而求出差值最大的绝对值。
说明:根据题目的要求首先需要构造一个二叉树,首先想到的是构造一个二叉搜索树,这样找就可以很方便的寻找到最大值和最小值,从而求出差值最大的绝对值。
#include <stdio.h> #include <malloc.h> typedef struct node { int data; struct node * lchild; struct node * rchild; }node; node * Insert(node *t , int key) { if(t == NULL) { node * p; p = (node *)malloc(sizeof(node)); p->data = key; p->lchild = NULL; p->rchild = NULL; t = p; } else { if(key < t->data) t->lchild = Insert(t->lchild, key); else t->rchild = Insert(t->rchild, key); } return t; } node * creat(node *t) { int i, n, key; scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d", &key); t = Insert(t, key); } return t; } int getMinValue(node* t){ node* p = t; while(p->lchild != NULL) p = p->lchild; return p->data; } int getMaxValue(node* t){ node* p = t; while(p->rchild != NULL) p = p->rchild; return p->data; } int getBigestDifference(node* t){ return getMaxValue(t) - getMinValue(t); } int main() { node * t = NULL; t = creat(t); printf("the bigest difference %d\n", getBigestDifference(t)); return 0; }
相关文章推荐
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析2
- 一个有趣的概率问题(阿里2012校园招聘——运维笔试)
- 去哪网2015年春季校园招聘笔试题
- 阿里巴巴2015年校园招聘技术(软开/测试)笔试题汇总
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析3
- 2015年欢聚时代(YY)校园招聘Java笔试题目
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析4
- 阿里巴巴2015年校园招聘笔试经历(题目与分析---倾情奉献)
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析5
- 2015年秋季腾讯校园招聘开发岗笔试题 四道大题之二
- 去哪儿网2015年春季校园招聘技术笔试题
- 2015年秋季腾讯校园招聘开发岗笔试题(四道大题)
- 2015年创新工场校园招聘软件研发岗位笔试题目——矩阵旋转
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析7
- 2015年秋季腾讯校园招聘开发岗笔试题 四道大题之一
- 2015年秋季腾讯校园招聘开发岗笔试题(四道大题)
- 2015年秋季腾讯校园招聘开发岗笔试题 四道大题三、四
- 2015年校园招聘之腾讯(数据挖掘)笔试面试题目
- 2015年阿里巴巴秋季校园招聘笔试题目
- 百度2015年校园招聘笔试题目