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;


}
};

 

posted @ 2013-05-25 10:57  NinaGood  阅读(89)  评论(0编辑  收藏  举报