您的位置:首页 > 编程语言 > C语言/C++

leetcode 日经贴,Cpp code -Maximum Depth of Binary Tree

2015-04-21 17:17 399 查看
Maximum Depth of Binary Tree

/**
* 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;
}
if (!root->left && !root->right) {
return 1;
}
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: