今天正式进入二叉树环节,感觉好久没碰二叉树了,上次碰二叉树还是大二的时候。今天收获还是挺多的。
二叉树的递归遍历
递归遍历的代码很简洁,但是第一次写的时候还是有点生疏,看了下视频马上做出来了。
二叉树前序遍历
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
TreeNode* current = root;
Traversal(current, result);
return result;
}
void Traversal(TreeNode* current, vector<int> &result){
if(!current)
return ;
result.push_back(current -> val); //存入根节点
Traversal(current -> left, result); //遍历左孩子节点
Traversal(current -> right, result); //遍历右孩子节点
}
};
二叉树中序遍历
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
TreeNode* current = root;
Traversal(current, result);
return result;
}
void Traversal(TreeNode* current, vector<int> &result){
if(!current)
return ;
Traversal(current -> left, result); //遍历左孩子节点
result.push_back(current -> val);
Traversal(current -> right, result); //遍历右孩子节点
}
};
二叉树后序遍历
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
TreeNode* current = root;
Traversal(current, result);
return result;
}
void Traversal(TreeNode* current, vector<int> &result){
if(!current)
return ;
Traversal(current -> left, result); //遍历左孩子节点
Traversal(current -> right, result); //遍历右孩子节点
result.push_back(current -> val);
}
};
二叉树的非递归遍历(非统一)
这个确实没学过,直接看视频,我在看完视频就感觉这个前序后序和中序代码风格不统一很难受,所以这个非统一的方法我并不打算重点掌握,看完视频以后自己敲出来就是了。
二叉树前序遍历
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
// TreeNode* current = root;
stack<TreeNode*> stk;
stk.push(root); //将根节点存入栈中
while(!stk.empty()){
TreeNode* node = stk.top();
stk.pop();
if(node != NULL)
result.push_back(node -> val);
else continue;
stk.push(node -> right);
stk.push(node -> left);
}
return result;
}
};
二叉树中序遍历
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> stk;
TreeNode* node = root;
while(node != NULL || !stk.empty()){
if(node != NULL){ //不为空则将节点压入栈中
stk.push(node);
node = node -> left;
}
else{ //为空则将上一级根节点弹出栈
node = stk.top();
stk.pop();
result.push_back(node -> val);
node = node -> right;
}
}
return result;
}
};
二叉树后序遍历
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> stk;
stk.push(root);
while(!stk.empty()){
TreeNode* node = stk.top();
stk.pop();
if(node != NULL)
result.push_back(node -> val);
else continue;
stk.push(node -> left);
stk.push(node -> right);
}
reverse(result.begin(), result.end());
return result;
}
};
二叉树的非递归遍历(统一)
我觉得这种统一的代码写法比较优美,需要重点掌握。这个没有视频教程,我直接对着代码啃的,感觉用统一方式实现三种遍历,核心在于node始终对应根节点,当根节点并未处理而只是访问时,一定要先弹出,根据遍历顺序将对应节点压入栈中,再重新只有在压入根节点时要额外压入一个NULL,在本次循环中,左右孩子节点无需压入NULL(因为在下次循环中,左右孩子节点会变成根节点)。
二叉树前序遍历
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> stk;
if(root != NULL)
stk.push(root);
while(!stk.empty()){
TreeNode* node = stk.top(); //node始终代表某一层的根节点
if(node != NULL){
stk.pop();
if(node -> right) stk.push(node -> right); //右孩子节点压栈
if(node -> left) stk.push(node -> left); //左孩子节点压栈
stk.push(node); //根节点压栈
stk.push(NULL);
}
else{ //遇到空节点,将栈内的数据弹出,存入数组
stk.pop();
result.push_back(stk.top() -> val);
stk.pop();
}
}
return result;
}
};
二叉树中序遍历
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> stk;
if(root != NULL)
stk.push(root);
while(!stk.empty()){
TreeNode* node = stk.top(); //node始终代表某一层的根节点
if(node != NULL){
stk.pop();
if(node -> right) stk.push(node -> right); //右孩子节点压栈
stk.push(node); //根节点压栈
stk.push(NULL);
if(node -> left) stk.push(node -> left); //左孩子节点压栈
}
else{ //遇到空节点,将栈内的数据弹出,存入数组
stk.pop();
result.push_back(stk.top() -> val);
stk.pop();
}
}
return result;
}
};
二叉树后序遍历
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> stk;
if(root != NULL)
stk.push(root);
while(!stk.empty()){
TreeNode* node = stk.top(); //node始终代表某一层的根节点
if(node != NULL){
stk.pop();
stk.push(node); //根节点压栈
stk.push(NULL);
if(node -> right) stk.push(node -> right); //右孩子节点压栈
if(node -> left) stk.push(node -> left); //左孩子节点压栈
}
else{ //遇到空节点,将栈内的数据弹出,存入数组
stk.pop();
result.push_back(stk.top() -> val);
stk.pop();
}
}
return result;
}
};
层序遍历
今天时间有限,层序遍历以前也确实没学过,看完视频刷了一道题就不打算做了,剩下的题明天补上。
102. 二叉树的层序遍历
主要借助队列来实现层序遍历,注意要用一个变量来维护当前层的待删除节点个数,不能直接用queue.size()来判断。其他的还挺简单的,看完一遍视频就把代码写出来了。
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> My_Queue;
if(root) My_Queue.push(root); //根节点不为空才入队
while(!My_Queue.empty()){
int size = My_Queue.size();
vector<int> temp;
while(size > 0){
TreeNode* node = My_Queue.front();
temp.push_back(My_Queue.front() -> val);
My_Queue.pop();
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
result.push_back(temp);
}
return result;
}
};
太累了,下播。
今天是8月27号,把层序遍历的剩余9道题都刷完了,程序的框架都大差不差,思路也很好想,没啥说的。直接贴代码。
107. 二叉树的层序遍历 II
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
int size = My_Queue.size();
vector<int> temp;
while(size > 0){
TreeNode* node = My_Queue.front();
My_Queue.pop();
temp.push_back(node -> val);
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
result.push_back(temp);
}
reverse(result.begin(), result.end());
return result;
}
};
199. 二叉树的右视图
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
vector<vector<int>> temp_result;
queue<TreeNode*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
vector<int> temp;
int size = My_Queue.size();
while(size > 0){
TreeNode* node = My_Queue.front();
My_Queue.pop();
temp.push_back(node -> val);
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
result.push_back(temp[temp.size() - 1]);
}
return result;
}
};
637. 二叉树的层平均值
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> result;
vector<vector<int>> temp_result;
queue<TreeNode*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
vector<int> temp;
int size = My_Queue.size();
long int sum = 0;
while(size > 0){
TreeNode* node = My_Queue.front();
My_Queue.pop();
temp.push_back(node -> val);
sum += node -> val;
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
result.push_back(sum / (double)temp.size());
}
return result;
}
};
429.N叉树的层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> result;
queue<Node*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
vector<int> temp;
int size = My_Queue.size();
while(size > 0){
Node* node = My_Queue.front();
My_Queue.pop();
temp.push_back(node -> val);
for(Node* Child : node -> children){
if(Child) My_Queue.push(Child);
}
size--;
}
result.push_back(temp);
}
return result;
}
};
515.在每个树行中找最大值
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> result;
queue<TreeNode*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
vector<int> temp;
int size = My_Queue.size();
while(size > 0){
TreeNode* node = My_Queue.front();
My_Queue.pop();
temp.push_back(node -> val);
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
result.push_back(*max_element(temp.begin(), temp.end()));
}
return result;
}
};
116. 填充每个节点的下一个右侧节点指针
class Solution {
public:
Node* connect(Node* root) {
Node* result = root;
queue<Node*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
// vector<int> temp;
int size = My_Queue.size();
while(size > 0){
Node* node = My_Queue.front();
My_Queue.pop();
if(size > 1)
node -> next = My_Queue.front();
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
// result.push_back(temp[temp.size() - 1]);
}
return result;
}
};
117. 填充每个节点的下一个右侧节点指针 II
class Solution {
public:
Node* connect(Node* root) {
Node* result = root;
queue<Node*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
int size = My_Queue.size();
while(size > 0){
Node* node = My_Queue.front();
My_Queue.pop();
if(size > 1)
node -> next = My_Queue.front();
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
}
return result;
}
};
104. 二叉树的最大深度
class Solution {
public:
int maxDepth(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
vector<int> temp;
int size = My_Queue.size();
while(size > 0){
TreeNode* node = My_Queue.front();
My_Queue.pop();
temp.push_back(node -> val);
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
size--;
}
result.push_back(temp);
}
return result.size();
}
};
111. 二叉树的最小深度
class Solution {
public:
int minDepth(TreeNode* root) {
int count = 0;
vector<vector<int>> result;
queue<TreeNode*> My_Queue;
if(root) My_Queue.push(root);
while(!My_Queue.empty()){
vector<int> temp;
count++;
int size = My_Queue.size();
while(size > 0){
TreeNode* node = My_Queue.front();
My_Queue.pop();
temp.push_back(node -> val);
if(node -> left) My_Queue.push(node -> left);
if(node -> right) My_Queue.push(node -> right);
if(!node -> left && !node -> right) return count;
size--;
}
result.push_back(temp);
}
return count;
}
};