【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 ...
随机推荐
- apt-get常见错误
安装错误:“E: Unmet dependencies.” 原因:非正常停止apt-get install * 错误提示:E: Unmet dependencies. Try 'apt-get - ...
- 【BZOJ 2982】 combination
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2982 [算法] lucas定理 [代码] #include<bits/stdc ...
- 【POJ 2286】 The Rotation Game
[题目链接] http://poj.org/problem?id=2286 [算法] IDA* [代码] #include <algorithm> #include <bitset& ...
- python print 显示不同的字体
显示格式: print('\033[显示方式;字体颜色;背景色m.....\033[0m') ------------------------------- 显示方式 | 效果 ----------- ...
- 第7章 性能和可靠性模式 Load-Balanced Cluster(负载平衡群集)
上下文 您已经决定在设计或修改基础结构层时使用群集,以便在能够适应不断变化的要求的同时保持良好的性能. 问题 在保持可接受的性能级别的同时,如何设计一个可适应负载变化的.可伸缩的基础结构层? 影响因素 ...
- 安装android驱动解决device not found
安装android驱动,解决device not found 0.1问题环境 windows10,android studio /eclipse,sdk ,jdk(其他windows也可用) 1.0 ...
- 关于出现Failed to instantiate SLF4J LoggerFactory问题原因,解决办法
在创建spring boot 文档进行配置的时候,因为使用spring boot 父级依赖的版本 <artifactId>spring-boot-starter-parent</ar ...
- PHP入门及服务环境配置(Nginx+PHP)
PHP入门及服务环境配置(Nginx+PHP) PHP入门 PHP维基百科: PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器")是一 ...
- 利用Axis2默认口令安全漏洞入侵WebService网站
近期,在黑吧安全网上关注了几则利用Axis2默认口令进行渗透测试的案例,大家的渗透思路基本一致,利用的技术工具也大致相同,我在总结这几则案例的基础之上进行了技术思路的拓展.黑吧安全网Axis2默认口令 ...
- VC++抛出自定义编译期异常的指令
#error 即可, 抛出消息是 #pragma message 最新的还有static_assert有一些用 一下子忘了网上居然搜不到...尝试了 关键字vc++.vc.vs.msvc + 抛出编 ...