LeetCode 104. Maximum Depth of Binary Tree
2016-03-22 15:38
405 查看
废话不多说,先看题目:
Given a binary tree, find its maximum depth.
题目意思:就是要求得到二叉树的深度或者高度
学过数据结构对这个肯定不会陌生,典型的递归:
Given a binary tree, find its maximum depth.
题目意思:就是要求得到二叉树的深度或者高度
学过数据结构对这个肯定不会陌生,典型的递归:
/** * Definition for a binary tree node. * 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==NULL) return 0; else { int l=maxDepth(root->left); int r=maxDepth(root->right); return r>l?r+1:l+1; } } };
相关文章推荐
- 在SQL Server中修改字段类型和字段名称的存储过程
- linux命令:nl命令
- 常见web UI 元素操作 及API使用
- 创建、导入、导出、复制以及粘贴 WMI 筛选器
- Android应用开发基础之十二:版本控制
- 最大公约数和最小公倍数
- ios 第三方分享组件
- Linux 在vim编辑器中显示行号
- 内存优化(大汇总——全)
- 键盘隐藏和显示的方法
- android:获取手机号码和姓名实现通讯录
- hashCode与equals的区别与联系
- 虚拟手术资料
- 从虚拟机copy到u盘的东西,在主机上不能用
- AppCompatActivity工具栏的设置(返回操作)
- [转载]一天只工作 3 小时的程序员,已坚持两年
- 快速排序
- 大神教你在Xen下快速添加自定义hypercallt超级调用
- hdu5646DZY Loves Partition
- HttpServletResponse对象(二)