所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。
思路分析:两道题都可以用层序遍历(迭代法)来做,遍历完一层深度变量depth就++。找最小深度实际上等价于找离根节点最近的叶子节点,我们在遍历每一个节点时,判断它的左右节点是否为空,若为空则为叶子节点,输出此时的depth。
找最大深度程序如下:
class Solution {
public:
// 找最大深度
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int Depth = 0, size = 0; // 根节点深度定义为1
while (!que.empty()) {
Depth++;
size = que.size(); // size必须固定, que.size()是不断变化的
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return Depth;
}
};
找最小深度程序如下:
class Solution2 {
public:
// 找最小深度
int minDepth(TreeNode* root) {
if (!root) return 0;
queue<TreeNode*> que;
que.push(root);
int Depth = 0, size = 0; // 根节点深度定义为1
while (!que.empty()) {
Depth++;
size = que.size(); // size必须固定, que.size()是不断变化的
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
if (node->left == NULL && node->right == NULL) return Depth;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return Depth;
}
};
当然,这道题也可以用递归法实现。一般的递归法我们需要注意三个点:
class Solution3 {
public:
// 找最大深度
int getDepth(TreeNode* root) {
if (!root) return 0;
int leftdepth = getDepth(root->left);
int rightdepth = getDepth(root->right);
int depth = 1 + max(leftdepth, rightdepth);
return depth;
}
int maxDepth(TreeNode* root) {
return getDepth(root);
}
};
精简版本如下:
class Solution3 {
public:
// 递归法找最大深度,简化版
int maxDepth(TreeNode* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
# include
# include
# include
# include
# include
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 maxDepth(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int Depth = 0, size = 0; // 根节点深度定义为1
while (!que.empty()) {
Depth++;
size = que.size(); // size必须固定, que.size()是不断变化的
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return Depth;
}
};
class Solution2 {
public:
// 找最小深度
int minDepth(TreeNode* root) {
if (!root) return 0;
queue<TreeNode*> que;
que.push(root);
int Depth = 0, size = 0; // 根节点深度定义为1
while (!que.empty()) {
Depth++;
size = que.size(); // size必须固定, que.size()是不断变化的
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
if (node->left == NULL && node->right == NULL) return Depth;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return Depth;
}
};
//class Solution3 {
//public:
// // 递归法找最大深度
// int getDepth(TreeNode* root) {
// if (!root) return 0;
// int leftdepth = getDepth(root->left);
// int rightdepth = getDepth(root->right);
// int depth = 1 + max(leftdepth, rightdepth);
// return depth;
// }
// int maxDepth(TreeNode* root) {
// return getDepth(root);
// }
//};
class Solution3 {
public:
// 递归法找最大深度,简化版
int maxDepth(TreeNode* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
void my_print(vector <string>& v, string msg)
{
cout << msg << endl;
for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
// 前序遍历迭代法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>&t, TreeNode* &node) {
if (t[0] == "NULL" || !t.size()) return; // 退出条件
else {
node = new TreeNode(stoi(t[0].c_str())); // 中
t.assign(t.begin() + 1, t.end());
Tree_Generator(t, node->left); // 左
t.assign(t.begin() + 1, t.end());
Tree_Generator(t, node->right); // 右
}
}
int main()
{
vector<string> t = { "3", "9", "NULL", "NULL", "20", "15", "NULL", "NULL", "7", "NULL", "NULL"}; // 前序遍历
my_print(t, "目标树");
TreeNode* root = new TreeNode();
Tree_Generator(t, root);
Solution3 s1;
int result = s1.maxDepth(root);
cout << "最大深度为:" << result << endl;
system("pause");
return 0;
}
end