forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.cpp
53 lines (53 loc) · 1.31 KB
/
solve.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
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
int minDepth2(struct TreeNode *root) {
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
if (root->left == NULL && root->right != NULL)
return minDepth(root->right) + 1;
if (root->left != NULL && root->right == NULL)
return minDepth(root->left) + 1;
return min(minDepth(root->left), minDepth(root->right));
}
int minDepth(struct TreeNode *root) {
if (root == NULL)
return 0;
if (root->left && root->right)
return min(minDepth(root->left), minDepth(root->right)) + 1;
else if (root->left)
return minDepth(root->left) + 1;
else
return minDepth(root->right) + 1;
}
};
void mk_children(TreeNode *root, int left, int right)
{
root->left = new TreeNode(left);
root->right = new TreeNode(right);
}
void mk_children(TreeNode *root, TreeNode *left, TreeNode *right)
{
root->left = left;
root->right = right;
}
int main(int argc, char **argv)
{
Solution solution;
TreeNode *root = new TreeNode(1);
mk_children(root, new TreeNode(2), nullptr);
cout << solution.minDepth(root) << endl;
return 0;
}