LeetCode:Maximum Depth of Binary Tree
2016-05-19 10:20
232 查看
Maximum Depth of Binary Tree
Total Accepted: 145230 Total
Submissions: 302450 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first
Search
Hide Similar Problems
(E) Balanced Binary Tree (E)
Minimum Depth of Binary Tree
c++ code:
/**
* 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) return 0;
else return max(maxDepth(root->left),maxDepth(root->right)) + 1;
}
// 自定义函数
int max(int x,int y) { return x>y?x:y; }
};
Total Accepted: 145230 Total
Submissions: 302450 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first
Search
Hide Similar Problems
(E) Balanced Binary Tree (E)
Minimum Depth of Binary Tree
c++ code:
/**
* 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) return 0;
else return max(maxDepth(root->left),maxDepth(root->right)) + 1;
}
// 自定义函数
int max(int x,int y) { return x>y?x:y; }
};
相关文章推荐
- Eclipse常用快捷键
- XML::Simple, XMLin()
- Canvas线段的绘制
- style
- delete 和 delete []的真正区别
- 一种保持顺序的Properties
- jquery显示隐藏元素的实现代码
- C++ 常用
- Tomcat 优化方案 和 配置详解
- Struts2常用注解
- xunsearch安装及环境检测(一)
- c#xml追加读取节点
- php代码开启缓冲的使用方法
- 对于pod导入第三方库文件终端语言记录
- Python之路【第二十三篇】爬虫
- 怎么面试架构师
- Linux下查看wav文件的头信息-sox
- linux设备驱动开发详解(基于4.0内核)_读书笔记二
- Except
- Android 代码混淆心得!