forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminDepth.txt
32 lines (32 loc) · 941 Bytes
/
minDepth.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Problem Link - https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
/**
* 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 d=INT_MAX;
int minDepth(TreeNode* root) {
if(root==NULL){
return 0;
}
if(root->left!=NULL and root->right!=NULL){
int l= minDepth(root->left);
int r= minDepth(root->right);
return min(l,r)+1;}
else if(root->left==NULL){
return minDepth(root->right)+1;
}
else if(root->right==NULL){
return minDepth(root->left)+1;
}
return 1;
}
};