Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- int minDepth(TreeNode* root) {
- if(root == nullptr) {
- return 0;
- } else {
- // return min( minDepth(root->left), minDepth(root->right)) + 1;
- int leftnum = 1e9, rightnum = 1e9;
- if(root->left == nullptr && root->right == nullptr) return 1;
- if(root->left != nullptr) leftnum = minDepth(root->left);
- if(root->right != nullptr) rightnum = minDepth(root->right);
- return min(leftnum, rightnum) + 1;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement