提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、104二叉树的最大深度
迭代法:
class Solution {
public:
int maxDepth(TreeNode* root) {
int depth = 0;
if (root == nullptr) return depth;
queue<TreeNode*> q;
q.push(root);
while (! q.empty()) {
int n = q.size();
while (n --) {
TreeNode* node = q.front();
q.pop();
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
depth ++;
}
return depth;
}
};
递归法:
class Solution {
public:
int getDepth(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int leftDepth = getDepth(node->left);
int rightDepth = getDepth(node->right);
return 1 + max(leftDepth, rightDepth);
}
int maxDepth(TreeNode* root) {
int res = getDepth(root);
return res;
}
};
二、559n叉树的最大深度
迭代法:
class Solution {
public:
int maxDepth(Node* root) {
int depth = 0;
if (root == nullptr) {
return depth;
}
queue<Node*> q;
q.push(root);
while (! q.empty()) {
int n = q.size();
while (n --) {
Node* node = q.front();
q.pop();
for (auto child: node->children) {
if (child != nullptr) {
q.push(child);
}
}
}
depth ++;
}
return depth;
}
};
递归法:
class Solution {
public:
int getDepth(Node* node) {
int res = 0;
if (node == nullptr) {
return res;
}
for (auto child: node->children) {
int childDepth = getDepth(child);
res = max(res, childDepth);
}
return 1 + res;
}
int maxDepth(Node* root) {
return getDepth(root);
}
};
三、111二叉树的最小深度
迭代法:
class Solution {
public:
int minDepth(TreeNode* root) {
int res = 0;
if (root == nullptr) return res;
queue<TreeNode*> q;
q.push(root);
while (! q.empty()) {
int n = q.size();
while (n --) {
TreeNode* node = q.front();
q.pop();
if (node->left == nullptr && node->right == nullptr) return res + 1;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
res ++;
}
return res;
}
};
递归法:
class Solution {
public:
int getDepth(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int leftDepth = getDepth(node->left);
int rightDepth = getDepth(node->right);
if (node->left == nullptr) return 1 + rightDepth;
if (node->right == nullptr) return 1 + leftDepth;
return 1 + min(leftDepth, rightDepth);
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};
四、222完全二叉树的节点个数
迭代法:
class Solution {
public:
int countNodes(TreeNode* root) {
if (root == nullptr) return 0;
queue<TreeNode*> q;
q.push(root);
int res = 0;
while (! q.empty()) {
TreeNode* node = q.front();
q.pop();
res ++;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
return res;
}
};
递归法:
class Solution {
public:
int recur(TreeNode* node) {
if (node == nullptr) return 0;
int leftNodes = recur(node->left);
int rightNodes = recur(node->right);
return 1 + leftNodes + rightNodes;
}
int countNodes(TreeNode* root) {
return recur(root);
}
};
以上两种写法都是当成普通二叉树解题,没有用到完全二叉树的特性。通过判断子树是不是满二叉树,利用公式计算节点数可以少遍历一些节点。
class Solution {
public:
int recur(TreeNode* node) {
if (node == nullptr) return 0;
TreeNode* left = node->left;
TreeNode* right = node->right;
int leftDepth = 0, rightDepth = 0;
while (left != nullptr) {
left = left->left;
leftDepth ++;
}
while (right != nullptr) {
right = right->right;
rightDepth ++;
}
if (leftDepth == rightDepth) return (2 << rightDepth) - 1;
return 1 + recur(node->left) + recur(node->right);
}
int countNodes(TreeNode* root) {
return recur(root);
}
};
总结
迭代法比较好想,递归法主要是要分清是遍历的顺序。