Skip to content

Commit ee6994e

Browse files
committed
Time: 3 ms (54.78%), Space: 146.8 MB (29.73%) - LeetHub
1 parent 1ecc771 commit ee6994e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int minDepth(TreeNode* root) {
15+
16+
if(root==nullptr) return 0;
17+
18+
if(root->left==nullptr && root->right==nullptr) return 1;
19+
20+
int left = root->left != nullptr ? minDepth(root->left) : INT_MAX;
21+
int right = root->right != nullptr ? minDepth(root->right) : INT_MAX;
22+
23+
return 1 + min(left,right);
24+
25+
}
26+
};

0 commit comments

Comments
 (0)