-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path155.h
36 lines (34 loc) · 838 Bytes
/
155.h
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
33
34
35
36
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: The root of binary tree
* @return: An integer
*/
int minDepth(TreeNode *root) {
// write your code here
if(root == nullptr) return 0;
return minDepth(root, 0);
}
int minDepth(TreeNode *root, int depth){
int d1 = INT_MAX, d2 = INT_MAX;
if(root->left == nullptr && root->right == nullptr)
return depth+1;
if(root->left)
d1 = minDepth(root->left, depth+1);
if(root->right)
d2 = minDepth(root->right, depth+1);
return min(d1, d2);
}
};