二叉树的深度
来源:互联网 发布:火鸟移动编程 编辑:程序博客网 时间:2024/05/16 09:30
输入一棵二叉树的根结点,就该树的深度。
struct BinaryTreeNode
{int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = NULL;
pNode->m_pRight = NULL;
return pNode;
}
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if(pParent != NULL)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
void DestroyTree(BinaryTreeNode* pRoot)
{
if(pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
int TreeDepth(BinaryTreeNode* pRoot)
{
if(pRoot == NULL)
return 0;
int nLeft = TreeDepth(pRoot->m_pLeft);
int nRight = TreeDepth(pRoot->m_pRight);
return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}
void Test(BinaryTreeNode* pRoot, int expected)
{
int result = TreeDepth(pRoot);
if(expected == result)
printf("Test passed.\n");
else
printf("Test failed.\n");
}
void Test1()
{
printf("Test1 begins.\n");
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
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);
}
void Test2()
{
printf("Test2 begins.\n");
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode1, pNode2, NULL);
ConnectTreeNodes(pNode2, pNode3, NULL);
ConnectTreeNodes(pNode3, pNode4, NULL);
ConnectTreeNodes(pNode4, pNode5, NULL);
Test(pNode1, 5);
DestroyTree(pNode1);
}
void Test3()
{
printf("Test3 begins.\n");
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode1, NULL, pNode2);
ConnectTreeNodes(pNode2, NULL, pNode3);
ConnectTreeNodes(pNode3, NULL, pNode4);
ConnectTreeNodes(pNode4, NULL, pNode5);
Test(pNode1, 5);
DestroyTree(pNode1);
}
void Test4()
{
printf("Test4 begins.\n");
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
Test(pNode1, 1);
DestroyTree(pNode1);
}
void Test5()
{
printf("Test5 begins.\n");
Test(NULL, 0);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
return 0;
}
阅读全文
0 0
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- atan和atan2
- Java File类
- 续写异常-(几个程序)
- (7)Date对象、with用法
- 基于selenium的pyse自动化测试框架
- 二叉树的深度
- centos远程升级sshd到5.9p1并删除老版本ssh
- Eclipse的Servers视图中无法添加Tomcat服务器
- Linux 连续执行多条命令的方法
- OpenSSH升级步骤及注意事项详解
- Maven的配置以及创建maven项目的注意事项
- HotSpotVM中垃圾回收的概念
- 指针典型例题
- as3.0和java服务端问题,如何让jiava立刻接收as发送的消息