[LeetCode] Maximum Depth of Binary Tree
2015-05-31 17:24
323 查看
这道题主要想说明频繁的函数调用时非常耗时的。
注意两段代码的第19行,第一段代码直接进行判断,第二段代码要调用max函数。执行时间如下图所示:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxDepth(TreeNode *root) { if( !root ) return 0; else{ int maxDepthLeft = maxDepth( root->left ); int maxDepthRight = maxDepth( root->right ); return (maxDepthLeft>maxDepthRight) ? (maxDepthLeft+1) : (maxDepthRight+1); } } };
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxDepth(TreeNode *root) { if( !root ) return 0; else{ int maxDepthLeft = maxDepth( root->left ); int maxDepthRight = maxDepth( root->right ); return max(maxDepthLeft, maxDepthRight) + 1; } } };
注意两段代码的第19行,第一段代码直接进行判断,第二段代码要调用max函数。执行时间如下图所示:
相关文章推荐
- 第十二周项目1 - 教师兼干部类
- 载波通信模块的代码分析
- jquery中extend的实现
- MAC下安装与配置MySQL
- Js-字符转换数字
- 第十二周项目1 - 教师兼干部类
- Skype for Business Server 2015-05-监控和存档服务器-配置
- gcc与g++的区别
- Cassandra ABC
- bash regrex operator =~
- 【C++总结】对象和类,this指针
- Java回忆录(八)--------异常处理
- mac的svn之cornerstone简易教程
- 黑马程序员——动态内存
- 沙美中文离线地图App上线
- Linux进程控制命令之ps
- OC视频笔记3.6(字符串常用方法)3.7(可变字符串的常用方法)
- HDU 2023 求平均成绩
- 结巴分词源代码解析(二)
- $.ajax()方法详解