【CareerCup】Trees and Graphs—Q4.3
转载请注明出处:http://blog.csdn.net/ns_code/article/details/24744177
题目:
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.
翻译:
给定一个有序数组(递增),敲代码构建一棵具有最小高度的二叉树。
思路:
要使二叉树的高度最小,则要尽量使其左右子树的节点数目相当,自然就考虑到将其构造成为二叉排序树,且将有序数组的中间大的数作为根节点,这样得到的二叉树的高度便是最小的。
实现代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct BTNode
{
int data;
struct BTNode *pLchild;
struct BTNode *pRchild;
}BTNode, *BTree; /*
依据给定的递增数组递归创建高度最小的二叉树,
由于要改动指向根节点的指针的指向,因此要传入pTree的指针。即BTNode的二级指针
*/
void createBTree(BTree *ppTree,int *A,int start,int end)
{
if(start <= end)
{
int mid = (start + end)/2;
*ppTree = (BTree)malloc(sizeof(BTNode));
if(*ppTree == NULL)
{
printf("malloc faild");
exit(EXIT_FAILURE);
}
(*ppTree)->data = A[mid];
(*ppTree)->pLchild = NULL;
(*ppTree)->pRchild = NULL;
createBTree(&(*ppTree)->pLchild,A,start,mid-1);
createBTree(&(*ppTree)->pRchild,A,mid+1,end);
}
} /*
返回两个整数的最大值
*/
int max(int a,int b)
{
return a>b? a:b;
} /*
求二叉树的深度
*/
int height(BTree pTree)
{
if(pTree == NULL)
return 0;
else
return max(height(pTree->pLchild),height(pTree->pRchild)) + 1;
} /*
中序遍历的递归实现
*/
void in_traverse(BTree pTree)
{
if(pTree)
{
if(pTree->pLchild)
in_traverse(pTree->pLchild);
printf("%d ",pTree->data);
if(pTree->pRchild)
in_traverse(pTree->pRchild);
}
} int main()
{
int A[] = {0,1,2,3,4,5,6,7};
int len = 8;
BTree pTree;
createBTree(&pTree,A,0,len-1);
printf("the height of this tree is %d\n",height(pTree));
printf("中序遍历后的结果为:\n");
in_traverse(pTree);
printf("\n");
return 0;
}
測试结果:
注:代码开源到Github:https://github.com/mmc-maodun/CareerCup
【CareerCup】Trees and Graphs—Q4.3的更多相关文章
- 【CF375D】Trees and Queries——树上启发式合并
(题面不是来自Luogu) 题目描述 有一个大小为n且以1为根的树,树上每个点都有对应的颜色ci.现给出m次询问v, k,问以v为根的子树中有多少种颜色至少出现了k次. 输入格式 第一行两个数n,m表 ...
- 【HDU4010】【LCT】Query on The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
[科普]什么是BestCoder?如何参加? Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
- 【CF724F】Uniformly Branched Trees 动态规划
[CF724F]Uniformly Branched Trees 题意:询问n个点的每个非叶子点度数恰好等于d的不同构的无根树的数目. $n\le 1000,d\le 10$. 题解:先考虑有根树的版 ...
- 【CF917D】Stranger Trees 树形DP+Prufer序列
[CF917D]Stranger Trees 题意:给你一棵n个点的树,对于k=1...n,问你有多少有标号的n个点的树,与给出的树有恰好k条边相同? $n\le 100$ 题解:我们先考虑容斥,求出 ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【动态规划】Codeforces 711C Coloring Trees
题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...
随机推荐
- Windows下搭建ffmpeg+VS2008开发环境详细教程【转】
本文转载自:http://www.voidcn.com/article/p-vxdntdgc-bkq.html 由于个人是从事音视频开发相关的工作,所以也把自己的一些过程写下来,方便大家以及自己查看, ...
- Google TPU 揭密——看TPU的架构框图,矩阵加乘、Pool等处理模块,CISC指令集,必然需要编译器
Google TPU 揭密 转自:https://mp.weixin.qq.com/s/Kf_L4u7JRxJ8kF3Pi8M5iw Google TPU(Tensor Processing Unit ...
- javascript必须知道的知识要点(一)
该文章不详细叙述各知识要点的具体内容,仅把要点列出来,供大家学习的时候参照,或者检测自己是否熟练掌握了javascript,清楚各个部分的内容. 语句 注释 输出 字面量 变量 数据类型 typeof ...
- C - Insomnia cure
Problem description «One dragon. Two dragon. Three dragon», — the princess was counting. She had tro ...
- B - Expression
Problem description Petya studies in a school and he adores Maths. His class has been studying arith ...
- Super超级ERP系统---(2)基础信息管理
这一节我我们来了解下super系统的基础信息模块有哪些功能以及怎么实现.任何功能再强大的系统,也需要基本信息来支撑.超级erp系统的基础信息主要有供应商管理,品牌管理,分类管理,商品管理几个模块构成. ...
- Fail2ban + firewalld 防护doss攻击
系统环境:centos7.3 用途:利用fail2ban+Firewalld来防CC攻击和SSH爆破 准备工作: 1.检查Firewalld是否启用 #如果您已经安装iptables建议先关闭 ser ...
- Web-数据库-mysql篇
DDL创造 create 修改 alter 删除 dorp DML插入 insert 删除 delete 更新 updateDQLselect from where MySQL与JDBC 一.用 ...
- jQuery操作DOM知识总结
jquery操作DOM(节点) 1.创建元素 //$(htmlStr) //htmlStr:html格式的字符串 $("<span>这是一个span元素</span> ...
- hdu3572Task Schedule 最大流,判断满流 优化的SAP算法
PS:多校联赛的题目质量还是挺高的.建图不会啊,看了题解才会的. 参考博客:http://blog.csdn.net/luyuncheng/article/details/7944417 看了上面博客 ...