输入一颗二叉树的根节点,求该树的深度

算法描述:

输入一颗二叉树的根节点,求该树的深度,从根节点到叶节点依次经过的结点形成一条路径,最长路径的长度为树的深度

算法实现:


/*************************************************************************
	> File Name: main.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月14日 星期四 21时04分09秒
 ************************************************************************/

#include "TreeDepth.h"
#include "BinaryTree.h"
void Test(struct BinaryTreeNode *pRoot, int expected)
{
	int result = TreeDepth(pRoot);
	if (result == expected)
		printf("test pass!\n");
	else
		printf("test fail!\n");
}
void Test1()
{
	printf("begin:\n");
	struct BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
	struct BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
	struct BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
	struct BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
	struct BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);
	struct BinaryTreeNode *pNode6 = CreateBinaryTreeNode(6);
	struct BinaryTreeNode *pNode7 = CreateBinaryTreeNode(7);

	ConnectTreeNodes(pNode1, pNode2, pNode3);
	ConnectTreeNodes(pNode2, pNode4, pNode5);
	ConnectTreeNodes(pNode3, NULL, pNode6);
	ConnectTreeNodes(pNode5, pNode7, NULL);

	Test(pNode1, 4);
	DestroyTree(pNode1);
	printf("end!\n");
}
int main()
{
	Test1();
	return 0;
}

/*************************************************************************
	> File Name: Tree.h
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月14日 星期四 20时34分23秒
 ************************************************************************/

#ifndef _TREE_H
#define _TREE_H
#include <stdio.h>
#include <stdlib.h>
struct BinaryTreeNode
{
	int value;
	struct BinaryTreeNode *left;
	struct BinaryTreeNode *right;
};
struct BinaryTreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight);
void PrintTreeNode(struct BinaryTreeNode *pNode);
void PrintTree(struct BinaryTreeNode *pRoot);
void DestroyTree(struct BinaryTreeNode *pRoot);

#endif

/*************************************************************************
	> File Name: Tree.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月14日 星期四 20时38分43秒
 ************************************************************************/

#include "BinaryTree.h"
struct BinaryTreeNode *CreateBinaryTreeNode(int value)
{
	struct BinaryTreeNode *pNode = (struct BinaryTreeNode *)malloc(sizeof(struct BinaryTreeNode));
	pNode->value = value;
	pNode->left = NULL;
	pNode->right = NULL;
	return pNode;
}

void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight)
{
	if (pRoot != NULL)
	{
		pRoot->left = pLeft;
		pRoot->right = pRight;
	}
}
void PrintTreeNode(struct BinaryTreeNode *pNode)
{
	if (pNode != NULL)
	{
		printf("the value of this node is :%d\n", pNode->value);
		if (pNode->left != NULL)
		{
			printf("the value of left child is %d\n",pNode->left->value);
		}
		else
		{
			printf("the left child is NULL\n");
		}
		if (pNode->right != NULL)
		{
			printf("the value of right child is %d\n",pNode->right->value);
		}
		else
		{
			printf("the right child is NULL\n");
		}
	}
	printf("\n");
}
void PrintTree(struct BinaryTreeNode *pRoot)
{
	PrintTreeNode(pRoot);
	if (pRoot != NULL)
	{
		if (pRoot->left != NULL)
			PrintTree(pRoot->left);
		if (pRoot->right != NULL)
			PrintTree(pRoot->right);
	}
}
void DestroyTree(struct BinaryTreeNode *pRoot)
{
	if (pRoot != NULL)
	{
		struct BinaryTreeNode *pLeft = pRoot->left;
		struct BinaryTreeNode *pRight = pRoot->right;

		free(pRoot);
		pRoot == NULL;
		DestroyTree(pLeft);
		DestroyTree(pRight);
	}
}
/*************************************************************************
	> File Name: TreeDepth.h
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月14日 星期四 20时55分04秒
 ************************************************************************/

#ifndef _TREEDEPTH_H
#define _TREEDEPTH_H
#include "BinaryTree.h"
int TreeDepth(struct BinaryTreeNode *pRoot);

#endif

/*************************************************************************
	> File Name: TreeDepth.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月14日 星期四 20时58分54秒
 ************************************************************************/
#include "BinaryTree.h"
#include "TreeDepth.h"
/*
 * 输入一颗二叉树的根节点,求该树的深度,从根节点到叶节点依次经过的结点形成一条路径,最长路径的长度为树的深度
 * */
int TreeDepth(struct BinaryTreeNode *pRoot)
{
	if ( pRoot == NULL )
		return 0;
	int numLeft = TreeDepth(pRoot->left);
	int numRight = TreeDepth(pRoot->right);

	return (numLeft > numRight)?(numLeft + 1):(numRight + 1);
}

CC = gcc
CFLAGS = -g -O2 -Wall

%.o:%.c
	$(CC) -o $@ -c $(CFLAGS) $<

main:main.o TreeDepth.o BinaryTree.o
	$(CC) main.o TreeDepth.o BinaryTree.o -o main $(CFLAGS)

clean:
	rm -rf *.o main



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值