Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- private:
- int min(int a, int b)
- {
- if (a < b)
- return a;
- return b;
- }
- int countDepth(TreeNode *cur)
- {
- if (cur->left == NULL || cur->right == NULL)
- {
- if (cur->left != NULL)
- return countDepth(cur->left) + 1;
- if (cur->right != NULL)
- return countDepth(cur->right) + 1;
- return 1;
- }
- return min(countDepth(cur->left), countDepth(cur->right)) + 1;
- }
- public:
- int minDepth(TreeNode* root) {
- if (root == NULL)
- return 0;
- return countDepth(root);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment