二叉树的深度
2015-06-11 17:33
253 查看
时间限制:1秒空间限制:32768K
通过比例:56.94%
最佳记录:0ms|8552K(来自 牛客688826号)
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
通过比例:56.94%
最佳记录:0ms|8552K(来自 牛客688826号)
题目描述
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: int TreeDepth(TreeNode* pRoot) { if(!pRoot) return 0; int maxdepth=0; LNRSearch(pRoot,1,maxdepth); return maxdepth; } void LNRSearch(TreeNode *root,int depth,int& max) { if(!root) return; LNRSearch(root->left,depth+1,max); if(depth>max) max=depth; LNRSearch(root->right,depth+1,max); } };
相关文章推荐
- jqueryUI的拖动说明
- MFC 不同工程的对话框如何复制过来?
- 几种调用协议的区别__pascal __cdecl __msfastcall __fastcall __stdcall
- iOS Mail.app inject kit
- SVN版本控制与分支设置
- Java5
- 用三种不同的方法访问Linux系统上的FTP服务过程详解 推荐
- LPC1788--SYSTEM--启动文件记录
- Auto Layout 使用心得(四)—— 22 行代码实现拖动回弹
- 模块[LCD]_LCD 调试总结
- Webservice SOAP传输序列化总结(转)
- Cocoa Touch的3种类的交流方式delegate/target/notification
- json解析的几种方式
- 「違憲」法制―また砂川とは驚きだ
- programming-challenges Crypt Kicker (110204) 题解
- Masonry介绍与使用实践 - iOS移动开发周报(转)
- linux 源码网站
- Auto Layout 使用心得(三)—— 自定义 cell 并使用 Auto Layout
- Auto Layout 使用心得(二)—— 实现三等分
- Android 软件开发之如何使用Eclipse Debug调试程序详解