每日一练(39) - 二叉树的深度
来源:互联网 发布:重启后自动还原的软件 编辑:程序博客网 时间:2024/05/16 11:48
题目来自剑指Offer
题目:求二叉树的深度
代码
#include <iostream>#include <assert.h>using namespace std;const int SIZE = 100;struct BinaryTreeNode {char m_nValue;BinaryTreeNode* m_pLeft;BinaryTreeNode* m_pRight;};int Depth(BinaryTreeNode* pRoot){if (!pRoot){return 0;}int nLeftDepth = Depth(pRoot->m_pLeft);int nRightDepth = Depth(pRoot->m_pRight);return max(nLeftDepth,nRightDepth) + 1;}void Create(BinaryTreeNode*& pRoot) { char newData; cin >> newData; if ('#' == newData) { pRoot = NULL; } else { pRoot = new BinaryTreeNode; pRoot->m_nValue = newData; Create(pRoot->m_pLeft); Create(pRoot->m_pRight); } } int main(){BinaryTreeNode* Root = NULL;Create(Root);cout<<"Depth: "<<Depth(Root)<<endl;system("pause");return 1;}注意:
树的构建类似树的子结构,这里不再详述。
- 每日一练(39) - 二叉树的深度
- 每日一题--二叉树的深度
- 每日一练(38) - 从上往下打印二叉树(二叉树的层次遍历)
- 每日一练(41) - 二叉搜索树的后序遍历序列
- 每日一练——N个节点的平衡二叉树有多少种形态
- 每日一练(40) - 判断二叉树是否是一颗平衡二叉树
- 每日一练——判断两序列是否为同一二叉搜索树序列
- 每日一练-----简单的计算器
- 算法每日一练
- 每日一练
- 每日一练
- 每日一练
- 每日一练
- 每日一练
- 每日一练
- 每日一练
- 每日一练
- 每日一练
- ASIHTTPRequest使用
- (转载)cocos2d-X学习之主要类介绍:精灵角色(CCSprite)
- hdu2111 Saving HDU (贪心)
- 重装win8后如何恢复ubuntu引导
- hdu1869-六度分离
- 每日一练(39) - 二叉树的深度
- 修改Cinder的Volume配额
- (转载)cocos2d-X学习之主要类介绍:动作:CCAction
- apk的反编译
- HiPer II----一体GNSS接收机
- Word文档格式,COM以及automation之间的关系
- 如何利用EXCEL生成任意自由度任意显著因子的F分布表
- 常用Windows消息、常量命名参考
- AJAX判断用户是否已注册