/**
* 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==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left==NULL&&root->right)
return maxDepth(root->right)+1;
else if(root->left&&root->right==NULL)
return maxDepth(root->left)+1;
else
{
int left=maxDepth(root->left);
int right=maxDepth(root->right);
return (left>right?left:right)+1;
}
}
};
* 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==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left==NULL&&root->right)
return maxDepth(root->right)+1;
else if(root->left&&root->right==NULL)
return maxDepth(root->left)+1;
else
{
int left=maxDepth(root->left);
int right=maxDepth(root->right);
return (left>right?left:right)+1;
}
}
};