[LeetCode]Maximum Depth of Binary Tree
2015-10-21 10:54
513 查看
题目描述:(链接)
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
解题思路:
递归, 深度优先算法!
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
解题思路:
递归, 深度优先算法!
class Solution { public: int maxDepth(TreeNode* root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } int leftDepth = maxDepth(root->left) + 1; int rightDepth = maxDepth(root->right) + 1; return leftDepth > rightDepth? leftDepth : rightDepth; } };
相关文章推荐
- 有边框的EditText的高度
- 单例模式的双层锁原理
- linux .deb 包的制作
- JMX简介
- Android程序监听已知包名的APK是否运行
- 文件透明加解密的实现层次
- Maven---leaning01
- Tomcat热部署方法(3种)
- 标准linu休眠和唤醒机制分析(四)
- win7下如何建立ftp服务器
- MATLAB中的分类器
- SVD与PCA
- struts2使用总结
- PHP中使用substr()截取字符串出现中文乱码问题该怎么办
- ADAS一些技术应用小结
- 【1012】畅通工程
- Session和Cookie
- Android之打造自己加载高清大图及瀑布流框架.解决错位等问题.
- 仿OpenStack开发云计算管理软件”--第1周:熟悉开发环境
- Asp.Net MVC 控制器