二叉树的深度
来源:互联网 发布:fifa18球员数据查询 编辑:程序博客网 时间:2024/05/10 18:12
题目描述
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
#include <iostream>#include "BinaryTree.h"using namespace std;int TreeDepth(BinaryTreeNode* pRoot){if(pRoot == NULL)return 0;int left = TreeDepth(pRoot ->m_pLeft);int right = TreeDepth (pRoot ->m_pRight);return (left > right) ? left + 1 : right + 1; }
// ====================测试代码====================void Test(BinaryTreeNode* pRoot, int expected){ int result = TreeDepth(pRoot); if(expected == result) printf("Test passed.\n"); else printf("Test failed.\n");}// 1// / \// 2 3// /\ \// 4 5 6// /// 7void 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);}// 1// /// 2// /// 3// /// 4// /// 5void 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);}// 1// \// 2// \// 3// \// 4// \// 5void 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);}// 树中只有1个结点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(int argc, char* argv[]){ Test1(); Test2(); Test3(); Test4(); Test5(); return 0;}
0 0
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- apache启动出错:ServerRoot must be a valid directory
- linux service命令
- Overview: (1)Data Structure: Interval Tree and Poj3264
- CSS文本属性(上)
- CCF-相反数-201403-1
- 二叉树的深度
- HDU1037 - Keep on Truckin' (简单题)
- 异常处理(try catch)
- 程序员养成记
- 提高django model效率的几个小方法
- [LinkerScript.1] 基本的链接器脚本概念 - Basic Linker Script Concepts
- 使用qsort进行字符串数组排序
- DIR 命令 详解
- CCF-出现次数最多的数-201312-1