转载请注明出处: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的更多相关文章

  1. 【CF375D】Trees and Queries——树上启发式合并

    (题面不是来自Luogu) 题目描述 有一个大小为n且以1为根的树,树上每个点都有对应的颜色ci.现给出m次询问v, k,问以v为根的子树中有多少种颜色至少出现了k次. 输入格式 第一行两个数n,m表 ...

  2. 【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 ...

  3. 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees

    [科普]什么是BestCoder?如何参加? Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  4. 【HDU1693】Eat the Trees(插头dp)

    [HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...

  5. 【CF724F】Uniformly Branched Trees 动态规划

    [CF724F]Uniformly Branched Trees 题意:询问n个点的每个非叶子点度数恰好等于d的不同构的无根树的数目. $n\le 1000,d\le 10$. 题解:先考虑有根树的版 ...

  6. 【CF917D】Stranger Trees 树形DP+Prufer序列

    [CF917D]Stranger Trees 题意:给你一棵n个点的树,对于k=1...n,问你有多少有标号的n个点的树,与给出的树有恰好k条边相同? $n\le 100$ 题解:我们先考虑容斥,求出 ...

  7. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  8. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. 【动态规划】Codeforces 711C Coloring Trees

    题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...

随机推荐

  1. JavaScript:DOM对象

    ylbtech-JavaScript:DOM对象 1. HTML DOM Document 对象返回顶部 1. HTML DOM Document 对象 HTML DOM 节点 在 HTML DOM ...

  2. linux IPtable防火墙 禁止和开放端口(转)

    linux IPtable防火墙 禁止和开放端口源:http://hi.baidu.com/zplllm/item/f910cb26b621db57c38d5983评: 1.关闭所有的 INPUT F ...

  3. javascript设计模式-单体模式

    场景:假设有一个Girl(美女)实体,该实体拥有姓名.年龄两个属性,拥有显示姓名和洗澡两个方法,下面分步骤构造该实体. 1.用简单基本单体模式: var Girl1 = { name:"昭君 ...

  4. LeetCode Weekly Contest 20

    1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...

  5. ADODB.RecordSet常用方法查询

    rs = Server.CreateObject("ADODB.RecordSet") rs.Open(sqlStr,conn,1,A) 注:A=1表示读取数据:A=3表示新增.修 ...

  6. Android中图片旋转

    Activity_main.xml文件配置 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...

  7. 数据结构——栈的实现(数组、Java)

    巩固数据结构 栈是一种有限制的线性表 只能对表尾进行操作 package com.shine.test.datastruct; import java.util.Arrays; public clas ...

  8. Bin文件

    那什么是bin文件呢?为什么这么关键? bin (binary)既是:二进制, 里面存放的一般是可执行的二进制文件.二进制即是机器代码,汇编语言编译后的结果.我们编译的是高级语言,把高级语言翻译为机器 ...

  9. 一个不错的学习android的网站

    http://androiddoc.qiniudn.com/guide/topics/ui/overview.html,最近想学下android的开发,找了一下网上的资料,中文的说的觉得太概括,看不太 ...

  10. C# 1.将整个文件夹复制到目标文件夹中 2.将指定文件复制到指定目标文件夹中

    ].Items.Clear(); string filePath = Application.StartupPath; string sourcePath = Path.Combine(filePat ...