您的位置:首页 > 其它

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

2016-04-15 09:27 507 查看
算法描述:

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

算法实现:

/*************************************************************************
> 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: