leetcode::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:
void preorder(TreeNode *t, int & max,int & sum)
{
if(t==NULL)
{
return ;
}
sum++;
if( sum > max )
{
max = sum;
}
if(t->left)
preorder( t->left,max,sum);
if(t->right)
preorder(t->right,max,sum);
sum--;
}
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int deep=0;
int sum=0;
if(root == NULL)
{
return deep;
}
preorder(root,deep,sum);
return deep;
}
};