-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum_depth_of_binary_tree.cpp
53 lines (49 loc) · 1.19 KB
/
minimum_depth_of_binary_tree.cpp
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
// =====================================================================================
//
// Filename: minimum_depth_of_binary_tree.cpp
//
// Description: 111. Minimum Depth of Binary Tree.
//
// Version: 1.0
// Created: 08/13/2019 11:25:21 AM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <algorithm>
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution
{
public:
int minDepth(TreeNode* root)
{
if (root == nullptr)
{
return 0;
}
int left = minDepth(root->left);
int right = minDepth(root->right);
if (left == 0 || right == 0)
{
return (left + right + 1);
}
return (std::min(left, right) + 1);
}
};
int main(int argc, char* argv[])
{
TreeNode* root = nullptr;
int depth = Solution().minDepth(root);
printf("Minimum depath: %d\n", depth);
return 0;
}