原题
思路
此题就是计算二叉树的最大深度,通过递归可以很轻松的进行处理
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) {
int ldepth, rdepth;
if (root != NULL)
{
ldepth = maxDepth(root->left);
rdepth = maxDepth(root->right);
if (ldepth > rdepth)
{
return ldepth + 1;
}
else
{
return rdepth + 1;
}
}
else
{
return 0;
}
}
};