数据结构-树-判断平衡二叉树

数据结构-树-判断平衡二叉树

题目描述

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。

示例

给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7

return true

给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4

return false

核心代码

Status BalanceBinaryTree(BiTree T, BiTree T1) {

	/*临接情况的递归出口*/
	if (T == NULL&&T1 == NULL)return TRUE; 
	if (T == NULL)
		return BalanceBinaryTree(T1->lchild, T1->rchild);
	if (T1 == NULL)
		return BalanceBinaryTree(T->lchild, T->rchild);

	/*递归函数*/
	if (abs(BiTreeDepth(T) - BiTreeDepth(T1)) > 1)
	{
		return FALSE;
	}
	
	/*递归出口、核心判断*/

	if (BalanceBinaryTree(T->lchild, T->rchild)&&BalanceBinaryTree(T1->lchild, T1->rchild)) {
		return TRUE;
	}
	else {
		return FALSE;
	}

}

可运行源代码

#include "string.h"
#include "stdio.h"    
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 100 /* 存储空间初始分配量 */

typedef int Status;		/* Status是函数的类型,其值是函数结果状态代码,如OK等 */

						/* 用于构造二叉树********************************** */
int index = 1;
int index1 = 1;
typedef char String[100]; /*  0号单元存放串的长度 */
String str, str1;

/*分配字符串*/
Status StrAssign(String T, char *chars)
{
	int i;
	/*strlen计算字符串的长度*/
	if (strlen(chars)>MAXSIZE)
		return ERROR;
	else
	{
		T[0] = strlen(chars);
		for (i = 1; i <= T[0]; i++)
			T[i] = *(chars + i - 1);
		return OK;
	}
}
/* ************************************************ */

typedef char TElemType;
TElemType Nil = ' '; /* 字符型以空格符为空 */

					 /*访问结点*/
Status visit(TElemType e)
{
	printf("%c ", e);
	return OK;
}

typedef struct BiTNode  /* 结点结构 */
{
	TElemType data;		/* 结点数据 */
	struct BiTNode *lchild, *rchild; /* 左右孩子指针 */
}BiTNode, *BiTree;


/* 构造空二叉树T */
Status InitBiTree(BiTree *T)
{
	*T = NULL;
	return OK;
}

/* 初始条件: 二叉树T存在。操作结果: 销毁二叉树T */
void DestroyBiTree(BiTree *T)
{
	if (*T)
	{
		if ((*T)->lchild) /* 有左孩子 */
			DestroyBiTree(&(*T)->lchild); /* 销毁左孩子子树 */
		if ((*T)->rchild) /* 有右孩子 */
			DestroyBiTree(&(*T)->rchild); /* 销毁右孩子子树 */
		free(*T); /* 释放根结点 */
		*T = NULL; /* 空指针赋0 */
	}
}

/* 按前序输入二叉树中结点的值(一个字符) */
/* #表示空树,构造二叉链表表示二叉树T。 */
void CreateBiTree(BiTree *T)
{
	TElemType ch;

	/* scanf("%c",&ch); */
	ch = str[index++];

	/*左右子树切换*/
	if (ch == '#')
		*T = NULL;
	else
	{
		/*生成树结点*/
		*T = (BiTree)malloc(sizeof(BiTNode));
		/*if (!*T)
		exit(OVERFLOW);*/
		(*T)->data = ch; /* 给结点赋值 */

						 /*递归创建左右子树*/
		CreateBiTree(&(*T)->lchild); /* 构造左子树 */
		CreateBiTree(&(*T)->rchild); /* 构造右子树 */
	}
}





/* 初始条件: 二叉树T存在。操作结果: 返回T的深度 */
int BiTreeDepth(BiTree T)
{
	int i, j;
	if (!T)
		return 0;
	if (T->lchild)
		/*递归调用*/
		i = BiTreeDepth(T->lchild);
	else
		i = 0;
	if (T->rchild)
		/*递归调用*/
		j = BiTreeDepth(T->rchild);
	else
		j = 0;
	return i>j ? i + 1 : j + 1;
}





Status BalanceBinaryTree(BiTree T, BiTree T1) {

	/*递归函数*/
	if (T == NULL&&T1 == NULL)return TRUE; 
	if (T == NULL)
		return BalanceBinaryTree(T1->lchild, T1->rchild);
	if (T1 == NULL)
		return BalanceBinaryTree(T->lchild, T->rchild);


	if (abs(BiTreeDepth(T) - BiTreeDepth(T1)) > 1)
	{
		return FALSE;
	}
	
	/*递归出口、核心判断*/

	if (BalanceBinaryTree(T->lchild, T->rchild)&&BalanceBinaryTree(T1->lchild, T1->rchild)) {
		return TRUE;
	}
	else {
		return FALSE;
	}

}

int main()
{
	int i;
	BiTree T;
	TElemType e1;


	/*初始化二叉树*/
	InitBiTree(&T);

	/*分配字符串*str=string,目的利用字符串给二叉树赋值*/
	/*前序输入,若不按规则输入,将报异常*/
	/*#代表空结点*/
	//StrAssign(str, "ABC##D##BD##C##");
//	StrAssign(str, "ABCD####E##");
	StrAssign(str, "ABC##D##BDAA###B##C##");

	/*创建二叉树*/
	CreateBiTree(&T);


	int k = BalanceBinaryTree(T->lchild, T->rchild);
	//DispBtree(T); 
	if (k == TRUE) {
		printf("TRUE");
	}
	else {
		printf("FALSE");
	}

	//DispBtree(T);
	/*销毁二叉树*/
	//DestroyBiTree(&T);

	system("pause");
}


运行结果

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值