102.二叉树的层序遍历
代码如下:
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) { //方法一:广度遍历
vector<vector<int>> ans;
queue<TreeNode*> q;
if (root == nullptr) {
return ans;
}
q.push(root);
while (!q.empty()) { //重要
int size = q.size(); //由于队列q的大小会在每一次for循环之后变化,所以需要每次变更队列大小。
vector<int> tempVec = {};
for (int i = 0; i < size; i++) {
TreeNode* tempNode = q.front();
q.pop();
tempVec.push_back(tempNode->val);
if (tempNode->left) {
q.push(tempNode->left);
}
if (tempNode->right) {
q.push(tempNode->right);
}
}
ans.push_back(tempVec);
}
return ans;
}
};
class Solution {
public:
void order(TreeNode* cur, vector<vector<int>>& result, int depth) //方法二:递归
{
if (cur == nullptr) return;
if (result.size() == depth) result.push_back(vector<int>());
result[depth].push_back(cur->val);
order(cur->left, result, depth + 1);
order(cur->right, result, depth + 1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
int depth = 0;
order(root, result, depth);
return result;
}
};
107.二叉树的层次遍历2
代码如下:
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) { //用广度遍历来做
queue<TreeNode*> q;
vector<vector<int>> ans;
if (root == nullptr) {
return ans;
}
q.push(root);
while (!q.empty()) {
int size = q.size();
vector<int> temp;
for (int i = 0; i < size; i++) {
TreeNode* curr = q.front();
temp.push_back(curr->val);
q.pop();
if (curr->left != nullptr) {
q.push(curr->left);
}
if (curr->right != nullptr) {
q.push(curr->right);
}
}
ans.push_back(temp);
}
reverse(ans.begin(), ans.end());
return ans;
}
};
199.二叉树的右视图
代码如下:
class Solution {
public:
vector<int> rightSideView(TreeNode* root) { //判断每次循环中是否为队列末尾的数,如果是,那就加入到vector中。
vector<int> ans;
queue<TreeNode*> q;
if (root == nullptr) {
return ans;
}
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* curr;
curr = q.front();
q.pop();
if (i == size - 1) {
ans.push_back(curr->val);
}
if (curr->left) {
q.push(curr->left);
}
if (curr->right) {
q.push(curr->right);
}
}
}
return ans;
}
};
637.二叉树的层平均值
代码如下:
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> q;
vector<double> ans;
if (root == nullptr) {
return ans;
}
q.push(root);
while (!q.empty()) {
double temp = 0;
double avg = 0;
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* curr = q.front();
q.pop();
temp += curr->val;
if (curr->left) {
q.push(curr->left);
}
if (curr->right) {
q.push(curr->right);
}
}
avg = temp / size;
ans.push_back(avg);
}
return ans;
}
};
429.N叉树的层序遍历
代码如下:
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) { //对于指针类,只能用->来指向对象,而不能用 .
queue<Node*> q;
vector<vector<int>> ans;
if (root == nullptr) {
return ans;
}
q.push(root);
while (!q.empty()) {
int size = q.size();
vector<int> temp;
for (int i = 0; i < size; i++) {
Node* curr = q.front();
q.pop();
temp.push_back(curr->val);
for (int j = 0; j < curr->children.size(); j++) {
if (curr->children[j]) {
q.push(curr->children[j]);
}
}
}
ans.push_back(temp);
}
return ans;
}
};
515.在每个数行中找最大值
代码如下:
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
queue<TreeNode*> q;
vector<int> ans;
if (root == nullptr) {
return ans;
}
q.push(root);
while(!q.empty()) {
int max = INT_MIN;
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* curr = q.front();
q.pop();
int temp = curr->val;
if (temp > max) {
max = temp;
}
if (curr->left) {
q.push(curr->left);
}
if (curr->right) {
q.push(curr->right);
}
}
ans.push_back(max);
}
return ans;
}
};
116.填充每个节点的下一个右侧节点指针
代码如下:
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != NULL) que.push(root);
while (!que.empty()) {
int size = que.size();
// vector<int> vec;
Node* nodePre;
Node* node;
for (int i = 0; i < size; i++) {
if (i == 0) {
nodePre = que.front(); // 取出一层的头结点
que.pop();
node = nodePre;
} else {
node = que.front();
que.pop();
nodePre->next = node; // 本层前一个节点next指向本节点
nodePre = nodePre->next;
}
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
nodePre->next = NULL; // 本层最后一个节点指向NULL
}
return root;
}
};
117.填充每个节点的下一个右侧节点指针2
代码如下:
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != NULL) que.push(root);
while (!que.empty()) {
int size = que.size();
// vector<int> vec;
Node* nodePre;
Node* node;
for (int i = 0; i < size; i++) {
if (i == 0) {
nodePre = que.front(); // 取出一层的头结点
que.pop();
node = nodePre;
} else {
node = que.front();
que.pop();
nodePre->next = node; // 本层前一个节点next指向本节点
nodePre = nodePre->next;
}
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
nodePre->next = NULL; // 本层最后一个节点指向NULL
}
return root;
}
};
104.二叉树的最大深度
代码如下:
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> q;
int depth = 0;
if (root == nullptr) {
return depth;
}
q.push(root);
while (!q.empty()) {
int size = q.size();
if (size) {
depth++;
}
for (int i = 0; i < size; i++) {
TreeNode* curr = q.front();
q.pop();
if (curr->left) {
q.push(curr->left);
}
if (curr->right) {
q.push(curr->right);
}
}
}
return depth;
}
};
111.二叉树的最小深度
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> q;
int depth = 0;
if (root == nullptr) {
return depth;
}
q.push(root);
while (!q.empty()) {
int size = q.size();
depth++;
for (int i = 0; i < size; i++) {
TreeNode* curr = q.front();
q.pop();
if (curr->left) {
q.push(curr->left);
}
if (curr->right) {
q.push(curr->right);
}
if (curr->left == nullptr && curr->right == nullptr) {
return depth;
}
}
}
return depth;
}
};
226.翻转二叉树
递归法代码如下:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) { //方法二:递归法
if (root == NULL) return root;
swap(root->left, root->right); // 中
invertTree(root->left); // 左
invertTree(root->right); // 右
return root;
}
};
遍历法代码如下:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) { //方法一:迭代法
stack<TreeNode*> st;
if (root == nullptr) {
return root;
}
st.push(root);
while (!st.empty()) {
TreeNode* curr = st.top();
st.pop();
swap(curr->left, curr->right);
if (curr->left) {
st.push(curr->left);
}
if (curr->right) {
st.push(curr->right);
}
}
return root;
}
};
101.对称二叉树
递归法代码如下:
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right) {
// 首先排除空节点的情况
if (left == NULL && right != NULL) return false;
else if (left != NULL && right == NULL) return false;
else if (left == NULL && right == NULL) return true;
// 排除了空节点,再排除数值不相同的情况
else if (left->val != right->val) return false;
// 此时就是:左右节点都不为空,且数值相同的情况
// 此时才做递归,做下一层的判断
bool outside = compare(left->left, right->right); // 左子树:左、 右子树:右
bool inside = compare(left->right, right->left); // 左子树:右、 右子树:左
bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理)
return isSame;
}
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
return compare(root->left, root->right);
}
};
迭代法代码如下:
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
queue<TreeNode*> que;
que.push(root->left); // 将左子树头结点加入队列
que.push(root->right); // 将右子树头结点加入队列
while (!que.empty()) { // 接下来就要判断这两个树是否相互翻转
TreeNode* leftNode = que.front(); que.pop();
TreeNode* rightNode = que.front(); que.pop();
if (!leftNode && !rightNode) { // 左节点为空、右节点为空,此时说明是对称的
continue;
}
// 左右一个节点不为空,或者都不为空但数值不相同,返回false
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
return false;
}
que.push(leftNode->left); // 加入左节点左孩子
que.push(rightNode->right); // 加入右节点右孩子
que.push(leftNode->right); // 加入左节点右孩子
que.push(rightNode->left); // 加入右节点左孩子
}
return true;
}
};
对称二叉树的代码写起来还是有困难,还要继续加油。
今毕。