Maximum Depth of Binary Tree
2015-04-16 13:01
141 查看
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.
教科书。。。
/**
* 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==0)
return 0;
return max(maxDepth(root->left), maxDepth(root->right))+1;
}
};
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
教科书。。。
/**
* 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==0)
return 0;
return max(maxDepth(root->left), maxDepth(root->right))+1;
}
};
相关文章推荐
- 《leetCode》: Maximum Depth of Binary Tree
- #111 Minimum Depth of Binary Tree & #104 Maximum Depth of Binary Tree
- 104 Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree JAVA
- leetcode Maximum Depth of Binary Tree
- leetcode -- Maximum Depth of Binary Tree
- LeetCode:Maximum Depth of Binary Tree
- Lintcode97 Maximum Depth Of BinaryTree solution 题解
- leetcode之Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree leetcode
- Leetcode | Minimum/Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree 树的最大深度
- [Leetcode]Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- LeetCode题解:Maximum Depth of Binary Tree
- LeetCode: Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree