-
Notifications
You must be signed in to change notification settings - Fork 1
/
111.minimum-depth-of-binary-tree.cpp
127 lines (110 loc) · 3.28 KB
/
111.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* @lc app=leetcode id=111 lang=cpp
*
* [111] Minimum Depth of Binary Tree
*/
// @lc code=start
/**
* 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) {}
* };
*/
#include <iostream>
#include <algorithm>
#include <stack>
#include <utility>
#include <queue>
using namespace std;
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 minDepth(TreeNode* root) {
if (root == nullptr)
{
return 0;
}
//1. recursive method
/*
Runtime: 12 ms, faster than 92.81% of C++ online submissions for Minimum Depth of Binary Tree.
Memory Usage: 20.4 MB, less than 5.03% of C++ online submissions for Minimum Depth of Binary Tree.
*/
if (root->left == nullptr && root->right == nullptr)
{
return 1;
}
if (!root->left)
{
return minDepth(root->right) + 1;
}
if (!root->right)
{
return minDepth(root->left) + 1;
}
return min(minDepth(root->left), minDepth(root->right)) + 1;
//2. Depth First Search(DFS)
stack<pair<TreeNode *, int>> stackNode;
stackNode.push(make_pair(root, 1));
int minRes = INT32_MAX;
while (!stackNode.empty())
{
TreeNode *curNode = stackNode.top().first;
int curDepth = stackNode.top().second;
stackNode.pop();
if (!curNode->left && !curNode->right)
{
minRes = min(minRes, curDepth);
}
if (curNode->left)
{
stackNode.push(make_pair(curNode->left, curDepth + 1));
}
if (curNode->right)
{
stackNode.push(make_pair(curNode->right, curDepth + 1));
}
}
return minRes;
//3. (BFS)
/*
Runtime: 24 ms, faster than 18.31% of C++ online submissions for Minimum Depth of Binary Tree.
Memory Usage: 19.8 MB, less than 95.73% of C++ online submissions for Minimum Depth of Binary Tree.
*/
queue<pair<TreeNode *, int>> curNodes;
curNodes.push(make_pair(root, 1));
int curRes = 0;
while (!curNodes.empty())
{
TreeNode *curNode = curNodes.front().first;
curRes = curNodes.front().second;
curNodes.pop();
if (!curNode->left && !curNode->right)
{
return curRes;
}
if (curNode->left)
{
curNodes.push(make_pair(curNode->left, curRes + 1));
}
if (curNode->right)
{
curNodes.push(make_pair(curNode->right, curRes + 1));
}
}
return -1;
}
};
// @lc code=end