【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,布布扣,bubuko.com

时间: 2024-08-15 05:57:38

【CareerCup】Trees and Graphs—Q4.3的相关文章

【DataStrcutre】Introduction and description of Binary Trees

[Definitions] Here is the recursive definition of a binary tree: A binary tree is either the empty set or a triple T = (x,L,R), where x is a node and L and R are disjoint binary trees, neither of which contains.  The node x is called the root of the 

【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 on a set of trees. There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them ef

【译】提升树算法的介绍(Introduction to Boosted Trees)

[译]提升树算法的介绍(Introduction to Boosted Trees) 1. 有监督学习的要素 XGBoost 适用于有监督学习问题.在此类问题中,我们使用多特征的训练数据集 \(x_i\) 去预测一个目标变量 \(y_i\) .在专门学习树模型前,我们先回顾一下有监督学习的基本要素. Elements of Supervised Learning XGBoost is used for supervised learning problems, where we use the

【HDU1693】Eat the Trees(插头dp)

[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版本吧... 因为可以任意分配哈密顿回路的数量,因此根本不需要再考虑插头的配对问题了,那么直接分情况转移就好啦. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #in

【题解】CF917D Stranger Trees(prufer序列+二项式反演)

[题解]CF917D Stranger Trees(prufer序列+二项式反演) 考虑有一个东西叫做\(prufer\)序列,然后个东西叫做图联通方案数. 然后我们考虑先算一下至少\(k\)条边在方案里的方案数,我们可以用树形dp \(dp(i,j,k)\)表示对于\(i\)节点及其子树,共有\(j\)条边被选中,且和\(i\)共有\(k\)个点是在一个联通块里的\(\prod siz[]\)之和.转移很简单啦.(但是CF的内存访问极慢!要用对内存友好的写法) 通过\(dp()\)可以很方便的

【leetcode】 Unique Binary Search Trees (middle)☆

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 找数字连续最大乘积子序列. 思路:这个麻烦在有负数和0,我的方法,如果有0,一切都设

【二叉树】hdu 1622 Trees on the level

[题意] 给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历 [思路] 注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置 [Accepted] #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<queue> using n

【DataStructure】Description and Introduction of Tree

[Description] At ree is a nonlinear data structure that models a hierarchical organization. The characteristic eatures are that each element may have several successors (called its "children") and every element except one (called the "root&

Python开发【前端】:jQuery

jQuery简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是"write Less,Do More",即倡导写更少的代码,做更多的事情.它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作.事件处理.动画设计和Ajax交互. jQuery的核心特性可以总结为:具有独特的链式语法和短小清晰的多功能接口: