这里写目录标题
题目:102. 二叉树的层序遍历
链接: leetcode题目链接
给你二叉树的根节点 root ,返回其节点值的 层序遍历。(即逐层地,从左到右访问所有节点)。
提示:
1).树中节点数目在范围 [0, 2000] 内
2).-1000 <= Node.val <= 1000
实现算法:层序遍历
队列先进先出,符合一层一层遍历的逻辑
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> result;
while (!que.empty()) {
int size = que.size();
vector<int> vec;
// 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
};
递归
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;
}
};
递归时不要忘了每层节点在结果中的输入,判断深度 depth 和二维向量的 size 关系,决定是再开一行还是直接输入。
p.s.
二维向量:
vector< vector < int >> matrix;
行:matrix.size();
列:matrix[0].size();
总数:行*列;
二维数组:
matrix[3][5]={};
总数:sizeof(matrix);
行:sizeof(matrix[0]);
列:总数/行;
题目:226. 翻转二叉树
链接: leetcode题目链接
给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
提示:
1).树中节点数目范围在 [0, 100] 内
2).-100 <= Node.val <= 100
实现算法:前序遍历/后序遍历/层序遍历
只要把每一个节点的左右孩子翻转一下,就可以达到整体翻转的效果
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;
}
};
p.s.
交换的是节点,用 swap 直接交换
自我实现
queue<TreeNode*> tree;
if(root!=NULL) tree.push(root);
while(!tree.empty()){
int len=tree.size();
while(len--){
TreeNode* cur=tree.front();
tree.pop();
if(cur->left) tree.push(cur->left);
if(cur->right) tree.push(cur->right);
swap(cur->left,cur->right);
}
}
return root;
p.s.
层序遍历不要总想着存储,对节点的操作不只有存储,还可以直接交换
做题心得
今天其实做了好多道层序的题,九道吧,有两道不太会,一个是,N叉树,一个是指向右侧节点,明天总结吧,对称二叉树用层序试了一下,好像不行,两个vector好像不能直接比较。
—— —— ——
在找最大值的时候,如何随便赋初值 max=0; 就有可能判断不了负数,可以用 int maxValue = INT_MIN;
表示整形最小整数。
题目:117. 填充每个节点的下一个右侧节点指针
链接: leetcode题目链接
*给定一个二叉树:
struct Node {
int val;
Node *left;
Node *right;
Node next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。
初始状态下,所有 next 指针都被设置为 NULL 。
提示:
1).树中的节点数在范围 [0, 6000] 内
2).-100 <= Node.val <= 100
实现算法:层序遍历
记录好每层的头指针
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;
}
};
p.s.
最后返回的仍然是根结点
自我实现
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> tree;
Node* node;
if(root!=NULL) tree.push(root);
while(!tree.empty()){
int len=tree.size();
Node* cur;
while(len--){
cur=tree.front();
tree.pop();
cur->next=tree.front();
if(cur->left) tree.push(cur->left);
if(cur->right) tree.push(cur->right);
}
cur->next=NULL;
}
return root;
}
};