首页 技术 正文
技术 2022年11月8日
0 收藏 704 点赞 1,999 浏览 2302 个字
// 面试题55(一):二叉树的深度
// 题目:输入一棵二叉树的根结点,求该树的深度。从根结点到叶结点依次经过的
// 结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。//如果左右节点只有一个存在,则深度为该存在的节点的深度+1
//如果左右节点都存在,则深度为最大子节点深度+1#include <iostream>
#include "BinaryTree.h"int TreeDepth(const BinaryTreeNode* pRoot)
{
if (pRoot == nullptr)
return ; int nLeft = TreeDepth(pRoot->m_pLeft);
int nRight = TreeDepth(pRoot->m_pRight); return (nLeft > nRight) ? (nLeft + ) : (nRight + );
}// ====================测试代码====================
void Test(const char* testName, const BinaryTreeNode* pRoot, int expected)
{
int result = TreeDepth(pRoot);
if (expected == result)
printf("%s passed.\n", testName);
else
printf("%s FAILED.\n", testName);
}// 1
// / \
// 2 3
// /\ \
// 4 5 6
// /
//
void Test1()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode1, pNode2, pNode3);
ConnectTreeNodes(pNode2, pNode4, pNode5);
ConnectTreeNodes(pNode3, nullptr, pNode6);
ConnectTreeNodes(pNode5, pNode7, nullptr); Test("Test1", pNode1, ); DestroyTree(pNode1);
}// 1
// /
// 2
// /
// 3
// /
// 4
// /
//
void Test2()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode1, pNode2, nullptr);
ConnectTreeNodes(pNode2, pNode3, nullptr);
ConnectTreeNodes(pNode3, pNode4, nullptr);
ConnectTreeNodes(pNode4, pNode5, nullptr); Test("Test2", pNode1, ); DestroyTree(pNode1);
}// 1
// \
// 2
// \
// 3
// \
// 4
// \
//
void Test3()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode1, nullptr, pNode2);
ConnectTreeNodes(pNode2, nullptr, pNode3);
ConnectTreeNodes(pNode3, nullptr, pNode4);
ConnectTreeNodes(pNode4, nullptr, pNode5); Test("Test3", pNode1, ); DestroyTree(pNode1);
}// 树中只有1个结点
void Test4()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
Test("Test4", pNode1, ); DestroyTree(pNode1);
}// 树中没有结点
void Test5()
{
Test("Test5", nullptr, );
}int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
system("pause");
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295