怎样编写一个程序,把一个有序整数数组放到二叉树中
来源:互联网 发布:js怎么给file控件赋值 编辑:程序博客网 时间:2024/05/24 02:29
分类: 数据结构 刀疤鸭 先疯盗骨2012-08-08 11:19 140人阅读 评论(0) 收藏 举报
structtree联想算法null
分析:本题考察二叉搜索树的建树方法,简单的递归结构。
关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。
关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。
而,学会把递归改称非递归也是一种必要的技术。
毕竟,递归会造成栈溢出,关于系统底层的程序中不到非不得以最好不要用。
但是对某些数学问题,就一定要学会用递归去解决。
- #include <stdio.h>
- #include <stdlib.h>
- struct student {
- int value;
- struct student *lchild;
- struct student *rchild;
- };
- void arraytotree(int *a, int len, struct student **p) {
- if(len) {
- *p = (struct student*)malloc(sizeof(struct student));
- (*p)->value = a[len/2];
- arraytotree(a, len/2, &((*p)->lchild));
- arraytotree(a+len/2+1, len-len/2-1, &((*p)->rchild));
- } else {
- *p = NULL;
- }
- }
- void display_tree(struct student *head) {
- if(head->lchild)display_tree(head->lchild);
- printf("%d\t", head->value);
- if(head->rchild)display_tree(head->rchild);
- }
- int main() {
- int a[] = {1,2,3,4,9,10,33,56,78,90};
- struct student *tree;
- arraytotree(a, sizeof(a)/sizeof(a[0]), &tree);
- printf("After convert:\n");
- display_tree(tree);
- printf("\n");
- return 0;
- }
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 怎样编写一个程序,把一个有序整数数组放到二叉树中(按行输出)
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 5、怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 86 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 编写一个程序,把一个有序整数数组放到二叉树中。
- 编写一个程序,把一个有序整数数组放到二叉树中
- 微软等数据结构+算法面试100题(5)--怎样编写一个程序,把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中? 编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
- 题目:把一个有序整数数组放到二叉树。
- static_cast<>揭密
- .Net三层架构
- 使用log4j时,为什么要写if (log.isDebugEnabled())
- linux资源-安全相关
- Train Problem
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 【Android Training - Graphics】高效地显示Bitmap图片(Lesson 4 - 优化Bitmap的内存使用)
- bfs+哈希表 + 三杯水
- JPA是什么?怎样开发JPA应用?
- AndEngine开发中的纹理,即texture
- BIOS 工作流程解析
- HDU4552(模拟)
- 从函数返回一个数组的2种方法 (C专家编程 p230)
- QProcess调用外部程序:QProcess::start与QProcess::execute