LeetCode:二叉树的属性(二叉树的最大最小深度)
LeetCode 二叉树刷题相关教程:
- LeetCode:二叉树的遍历方式
- LeetCode:二叉树的属性(对称二叉树)
- LeetCode:二叉树的属性(二叉树的最大最小深度)
- 待更新…
划重点: 使用前序求的就是深度,使用后序求的是高度。根节点的高度就是二叉树的最大深度。
104. 二叉树的最大深度
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
class Solution {
public:
int maxDepth(TreeNode* root) {
return getDepth(root);
}
int getDepth(TreeNode* root) {
if (root == nullptr) return 0;
int leftDepth = getDepth(root->left);
int rightDepth = getDepth(root->right);
return 1 + max(leftDepth, rightDepth);
}
};
class Solution {
public:
int maxDepth(TreeNode* root) {
result = 0;
if (root == nullptr) return result;
getDepth(root, 1);
return result;
}
void getDepth(TreeNode* node, int depth) {
result = depth > result ? depth : result; // 中
if (node->left == nullptr && node->right == nullptr) {
return;
}
if (node->left) {
getDepth(node->left, depth + 1);
}
if (node->right) {
getDepth(node->right, depth + 1);
}
return;
}
int result = 0;
};
使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
class Solution {
public:
int maxDepth(TreeNode* root) {
int depth = 0;
queue<TreeNode*> que;
if (root != nullptr) que.push(root);
while (!que.empty()) {
int size = que.size();
depth++;
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;
}
};
559. N 叉树的最大深度
给定一个 N 叉树,找到其最大深度。最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔。
class Solution {
public:
int maxDepth(Node* root) {
return getDepth(root);
}
int getDepth(Node* node) {
int depth = 0;
if (node == nullptr) return 0;
/*for (vector<Node*>::iterator it = node->children.begin(); it != node->children.end(); it++) {
depth = max(getDepth(*it), depth);
}*/
for (int i = 0; i < node->children.size(); i++) {
depth = max(getDepth(node->children[i]), depth);
}
return depth + 1;
}
};
class Solution {
public:
int maxDepth(Node* root) {
if (root == nullptr) return 0;
queue<Node*> que;
int depth = 0;
que.push(root);
while (!que.empty()) {
int size = que.size();
depth++;
for (int i = 0; i < size; i++) {
Node* node = que.front();
que.pop();
for (int j = 0; j < node->children.size(); j++) {
if (node->children[j]) {
que.push(node->children[j]);
}
}
}
}
return depth;
}
};
111. 二叉树的最小深度
给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
class Solution {
public:
int minDepth(TreeNode* node) {
if (node == nullptr) return 0;
if (node->left && !node->right) { // 当一个左子树为空,右不为空,这时并不是最低点
return 1 + minDepth(node->left);
}
if (!node->left && node->right) { // 当一个右子树为空,左不为空,这时并不是最低点
return 1 + minDepth(node->right);
}
return 1 + min(minDepth(node->left), minDepth(node->right));
}
};
迭代法:
class Solution {
public:
int minDepth(TreeNode* root) {
int depth = 0;
queue<TreeNode*> que;
if (root != nullptr) que.push(root);
while (!que.empty()) {
int size = que.size();
depth++;
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);
if (!node->left && !node->right) return depth;
}
}
return depth;
}
};
以上题解大多来自【代码随想录】,在此基础上做了一定总结,并附带一些自己的理解。
后续题目,随缘更新,有错误请指出!
END