leetcode Maximum Depth of Binary Tree

代码:

/**

 * 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 max;



 void deepSearch(TreeNode * root, int depth)

 {

     

     if (root != NULL)

     {

         deepSearch(root->left, depth + 1);

         deepSearch(root->right, depth + 1);

     }

     else

     {

         if (depth > max)

         {

             max = depth;

         }

     }

 }



 int maxDepth(struct TreeNode* root) 

 {

     if(root==NULL)

     return 0;

     max = 0;

     deepSearch(root, 0);

     return max;

 }

};

 

你可能感兴趣的:(LeetCode)