一、104. 二叉树的最大深度
层序遍历求最大深度
每一层i++即可,很好理解。
class Solution {
public:
int maxDepth(TreeNode* root) {
int i=0;
queue<TreeNode*> que;
if (root != NULL) que.push(root);
while (!que.empty()) {
int size = que.size();
// 这里一定要使用固定大小size,不要使用que.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);
}
i++;
}
return i;
}
};
后序遍历
关于递归还是有些不熟悉
class Solution {
public:
int getMaxDepth(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int left = getMaxDepth(node->left);
int right = getMaxDepth(node->right);
return 1 + max(left, right);
}
int maxDepth(TreeNode* root) {
return getMaxDepth(root);
}
};
先序遍历
class Solution {
public:
int result; //用全局变量记录答案
void getMaxDepth(TreeNode* node, int depth) { // 前序遍历
result = result > depth ? result : depth;// 中
if (!node->left && !node->right) {
return;
}
if (node->left) { // 左
depth++; // 深度加一
getMaxDepth(node->left, depth);
depth--; // 回溯
}
if (node->right) { // 右
depth++; // 深度加一
getMaxDepth(node->right, depth);
depth--; // 回溯
}
return ;
}
int maxDepth(TreeNode* root) {
result = 0;
if (root == nullptr) {
return 0;
}
getMaxDepth(root, 1);
return result;
}
};
二、559. N 叉树的最大深度
层序遍历 (迭代)
class Solution {
public:
int maxDepth(Node* root) { // 层序遍历
queue <Node*> que;
if (root) {
que.push(root);
}
int i = 0;
while (que.empty() != true) {
int size = que.size();
i++;
for(int i=0; i<size; i++) {
Node* node = que.front();
que.pop(); // 当前队头出队
for(int j=0; j<node->children.size(); j++) {
que.push(node->children[j]); // 把所有孩子入队
}
}
}
return i;
}
};
目前而言层序遍历已经非常熟悉了,还要多练习递归。
递归
class Solution {
public:
int maxDepth(Node* root) { // 先根遍历
if (!root) {
return 0;
}
int depth = 0;
for (int i=0; i < root->children.size(); i++) {
depth = max(depth, maxDepth(root->children[i]));
}
return 1 + depth;
}
};
三、111. 二叉树的最小深度
层序遍历
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int cnt = 0;
while (!que.empty()) {
int size = que.size();
TreeNode* node;
cnt++;
// 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
for (int i = 0; i < size; i++) {
node = que.front();
que.pop();
if(!node->left && !node->right) return cnt;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return cnt;
}
};
递归
最小深度是从根节点到最近叶子节点的最短路径上的节点数量!!!
class Solution {
public:
int getMinDepth(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int left = getMinDepth(node->left);
int right = getMinDepth(node->right);
// 左子树为空, 右子树非空, 则返回右子树的深度 + 1
if (node->left == nullptr && node->right) {
return 1 + right;
}
// 右子树为空, 左子树非空, 则返回左子树的深度 + 1
if (node->left && node->right == nullptr) {
return 1 + left;
}
return 1 + min(left, right);
}
int minDepth(TreeNode* root) {
return getMinDepth(root);
}
};
四、222. 完全二叉树的节点个数
层序遍历
class Solution {
public:
int countNodes(TreeNode* root) {
queue <TreeNode*> que;
int result = 0;
if (root) {
que.push(root);
}
while (que.empty() != true) {
int size = que.size();
for(int i=0; i<size; i++) {
TreeNode* node = que.front();
que.pop();
result++;
if (node->left) {
que.push(node->left);
}
if (node->right) {
que.push(node->right);
}
}
}
return result;
}
};
递归
全局变量
class Solution {
public:
int result;
void preOrder(TreeNode* node) {
if (node == nullptr) {
return ;
}
result++;
preOrder(node->left);
preOrder(node->right);
}
int countNodes(TreeNode* root) {
result = 0;
preOrder(root);
return result;
}
};
函数返回
class Solution {
private:
int getNodesNum(TreeNode* cur) {
if (cur == NULL) return 0;
int leftNum = getNodesNum(cur->left); // 左
int rightNum = getNodesNum(cur->right); // 右
int treeNum = leftNum + rightNum + 1; // 中
return treeNum;
}
public:
int countNodes(TreeNode* root) {
return getNodesNum(root);
}
};
留下一个时间复杂度O(log n × log n)的二刷来学
五、总结
其实目前来说掌握几个递归和层序遍历就差不多,现在刷了这么多题已经很熟悉了。
(3小时)