1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int max=-1; 13 void meng(TreeNode*root,int dep) 14 { 15 if((root->left==NULL)&&(root->right==NULL)) 16 { 17 if(dep>max) 18 max=dep; 19 return; 20 } 21 if(root->left) 22 meng(root->left,dep+1); 23 if(root->right) 24 meng(root->right,dep+1); 25 } 26 int maxDepth(TreeNode *root) { 27 // Start typing your C/C++ solution below 28 // DO NOT write int main() function 29 max=-1; 30 if(root==NULL) 31 return 0; 32 meng(root,1); 33 return max; 34 } 35 };