-
Notifications
You must be signed in to change notification settings - Fork 2
/
q0111.c
61 lines (57 loc) · 1.05 KB
/
q0111.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// q0111.c
// LeetCode
//
// Created by NowOrNever on 31/10/2019.
// Copyright © 2019 DoubleL. All rights reserved.
//
#include "q0111.h"
#include "../common.h"
//111. Minimum Depth of Binary Tree
//Easy
//
//883
//
//501
//
//Favorite
//
//Share
//Given a binary tree, find its minimum depth.
//
//The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
//
//Note: A leaf is a node with no children.
//
//Example:
//
//Given binary tree [3,9,20,null,null,15,7],
//
// 3
// / \
// 9 20
// / \
// 15 7
//return its minimum depth = 2.
//
//Accepted
//336,593
//Submissions
//932,645
int minDepth(struct TreeNode* root){
if (root) {
if (!root->left) {
return minDepth(root->right) + 1;
}
if (!root->right) {
return minDepth(root->left) + 1;
}
int left = minDepth(root->left);
int right = minDepth(root->right);
return (left < right ? left : right) + 1;
}
return 0;
}
int question111(){
return 0;
}