tanchukw

Untitled

Sep 7th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. class Solution {
  2. private:
  3.     int min(int a, int b)
  4.     {
  5.         if (a < b)
  6.             return a;
  7.         return b;
  8.     }
  9.     int countDepth(TreeNode *cur)
  10.     {
  11.         if (cur->left == NULL || cur->right == NULL)
  12.         {
  13.             if (cur->left != NULL)
  14.                 return countDepth(cur->left) + 1;
  15.             if (cur->right != NULL)
  16.                 return countDepth(cur->right) + 1;
  17.             return 1;
  18.         }
  19.         return min(countDepth(cur->left), countDepth(cur->right)) + 1;
  20.     }
  21. public:
  22.     int minDepth(TreeNode* root) {
  23.         if (root == NULL)
  24.             return 0;
  25.         return countDepth(root);
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment