《剑指offer》第五十五题(二叉树的深度)
// 面试题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 ;
}
《剑指offer》第五十五题(二叉树的深度)的更多相关文章
- 剑指Offer(二十五):复杂链表的复制
剑指Offer(二十五):复杂链表的复制 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...
- 剑指Offer(三十五):数组中的逆序对
剑指Offer(三十五):数组中的逆序对 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...
- 《剑指offer》第二十五题(合并两个排序的链表)
// 面试题25:合并两个排序的链表 // 题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按 // 照递增排序的.例如输入图3.11中的链表1和链表2,则合并之后的升序链表如链 ...
- 《剑指offer》第十五题(二进制中1的个数)
// 面试题:二进制中1的个数 // 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如 // 把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. #inclu ...
- 《剑指offer》第二十六题(树的子结构)
// 面试题26:树的子结构 // 题目:输入两棵二叉树A和B,判断B是不是A的子结构. #include <iostream> struct BinaryTreeNode { doubl ...
- 《剑指offer》第十九题(正则表达式匹配)
// 面试题19:正则表达式匹配 // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符'.' // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题 ...
- 《剑指offer》第二十九题(顺时针打印矩阵)
// 面试题29:顺时针打印矩阵 // 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. #include <iostream> void PrintMatrixInC ...
- 《剑指offer》第二十八题(对称的二叉树)
// 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...
- 《剑指offer》第二十四题(反转链表)
// 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...
- 《剑指offer》第二十二题(链表中倒数第k个结点)
// 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...
随机推荐
- Linux环境jdk的安装
1.下载jdk1.7,oracle的下载地址已经失效,找了个其他的地址进行下载. wget http://pc.xzstatic.com/2017/03/jdk7u79linuxx64.tar.gz ...
- Django中程序中图片资源的路径问题(static文件夹的放置)
步骤1:在settings.py文件的最后加上以下内容: STATIC_URL = '/static/'STATIC_ROOT = os.path.join(os.path.dirname(__fil ...
- jquery的$post方法不发送空数组的解决办法
问题:jquery里的ajax在提交post请求时,如果数据里有一个空数组,则这个空数组不会提交上去 技术上的解决办法如下: 源代码: var params = { type : , ids:[] } ...
- nlp homework 03
NLP Homework 03 --冯煜博 题目描述 (盒子和球模型)假设有3个盒子,每个盒子里装有红白两种颜色的球,盒子里的红白球有下表列出,初始状态分布. 解答 1. 给出HMM模型 \(\mu= ...
- 维护keepalived与mysql漂移脚本
环境拓扑 chengAlived #!/bin/bash function checkModelone(){ echo "重新进行获取" wget 192.168.158.147: ...
- 【Python58--正则2】
一.字符匹配 1.元字符:完整列表:. ^ $ * + ? { } [ ] \ | ( ) 元字符 描述 .点 匹配除换行符外任意一个字符 x|y 匹配 x 或 ...
- 【python010-数组】
1.创建列表 *创建普通列表 >>> member = ['尘封','破冰','python']>>> member['尘封', '破冰', 'python'] * ...
- 2018年11月16日 我和SB交流有代沟-继续字符串4
test="abcdeffedcba" v=test.lstrip("bcabc")#寻找的是最多匹配然后移除指定字符串 print("1.lstri ...
- topcoder srm 420 div1
problem1 link 暴力即可.因为即便所有数字的和是50,50所有的不同的划分数只有204226中.所以最长的循环也就这么大. problem2 link 令$f[i][j]$表示有$i$个红 ...
- .NET BackgroundWorker的一般使用方式
代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...