1、二叉树递归遍历
1.1、先序(根左右)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void preOrder(TreeNode* root,vector<int> & res){
if(!root) return;
res.push_back(root->val);
preOrder(root->left,res);
preOrder(root->right,res);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int>res;
preOrder(root,res);
return res;
}
};
// 1.2、中序(左根右)
class Solution {
public:
void inOrder(TreeNode * root,vector<int>& res) // 递归函数
{
if(!root) // 如果此节点为空,返回
return;
inOrder(root->left,res); // 递归左节点,
res.push_back(root->val); // 把当前值写入结果集
inOrder(root->right,res); //递归右节点
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res; // 初始化结果集
inOrder(root,res); // 递归函数
return res;
}
};
// 1.3、后序(左右根)
class Solution {
public:
void postOrder(TreeNode * root,vector<int>& res){
if(!root) return ;
postOrder(root->left,res);
postOrder(root->right,res);
res.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
postOrder(root,res);
return res;
}
};
2、非递归遍历(前序根左右,后序左右根)
// 2.1、前序(根左右)
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int>res;
if(!root) return res;
stack<TreeNode*>st;
st.push(root);
while(!st.empty()){
auto tem = st.top();
st.pop();
res.push_back(tem->val);
if(tem->right) st.push(tem->right); // 这里不要写错:(!tem->right)
if(tem->left) st.push(tem->left);
}
return res;
}
// 2.3、后序(左右根)
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
if(!root) return res;
stack<TreeNode*>st;
st.push(root);
while(!st.empty()){
auto tem = st.top();
st.pop();
res.push_back(tem->val);
if(tem->left) st.push(tem->left);
if(tem->right) st.push(tem->right);
}
reverse(res.begin(),res.end());
return res;
}
};
2.4、层序非递归遍历二叉树
使用队列结构,
题目链接
auto tem = que.front();
que.pop();
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> levelOrder(TreeNode* root) {
vector<int>res;
if(!root) return res;
queue<TreeNode*>que;
que.push(root);
while(!que.empty()){
auto tem = que.front();
que.pop();
res.push_back(tem->val);
if(tem->left) que.push(tem->left);
if(tem->right) que.push(tem->right);
}
return res;
}
};
2.4.4 从下往上打印:
使用一下vector的逆序函数
reverse(res.begin(),res.end());
2.5、之字形打印二叉树
题目链接
使用两层vector
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(!root) return res; // 特判
int flag = 0;
queue<TreeNode*>que;
que.push(root);
while(!que.empty()){
int n = que.size();
vector<int>nums; // 局部nums所以不用在最后重置为0
while(n--){
auto tem = que.front();
que.pop();
nums.push_back(tem->val);
if(tem->left) que.push(tem->left);
if(tem->right) que.push(tem->right);
}
if(flag % 2 == 1){
reverse(nums.begin(),nums.end());
}
res.push_back(nums);
flag++;
}
return res;
}
};