一、二叉树的三种遍历(递归版)
- 思路
- 确定递归函数的参数以及返回类型
- 确定递归终止条件
- 确定单层逻辑
- 前序遍历
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> resu;
preorder(resu, root);
return resu;
}
void preorder(vector<int>& resu, TreeNode* root){
if(root == nullptr) return;
resu.push_back(root->val);
preorder(resu, root->left);
preorder(resu, root->right);
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> resu;
inorder(resu, root);
return resu;
}
void inorder(vector<int>& resu, TreeNode* root){
if(root == nullptr) return;
inorder(resu,root->left);
resu.push_back(root->val);
inorder(resu,root->right);
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> resu;
postorder(resu, root);
return resu;
}
void postorder(vector<int>& resu, TreeNode* root){
if(root == nullptr) return;
postorder(resu,root->left);
postorder(resu,root->right);
resu.push_back(root->val);
}
};
二、二叉树的三种遍历(迭代版)
- 前序遍历
- 中-左-右
- 将根节点入栈。将根节点出栈表示访问该节点,即输出其值,再将右孩子入栈,最后将左孩子入栈。栈有后进先出的特点,此时后进的左孩子先出栈也就先被访问。
- 不断迭代直到栈为空表示遍历完毕。
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> resu;
stack<TreeNode*> stk;
if(root==NULL) return resu;
stk.push(root);
while(!stk.empty()){
TreeNode* tmp = stk.top();
resu.push_back(tmp->val);
stk.pop();
if(tmp->right) stk.push(tmp->right);
if(tmp->left) stk.push(tmp->left);
}
return resu;
}
};
- 中序遍历
- 左-中-右
- cur指向当前访问的节点,stk.top()为当前要处理的节点
- 先寻找整棵树最左边的节点,即如果cur有左孩子,就一直向左寻找,同时将访问过的节点入栈。当cur为空时表示已经到达了尽头,此时栈顶元素为整棵树最左边的节点,将cur指向该节点。将他出栈并将数据加入到结果向量中。此时将cur指向该节点的右孩子,如果不为空则进行寻找该节点右子树的最左边的节点。
- 直到栈为空或cur为空表示遍历完毕
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> resu;
stack<TreeNode*> stk;
TreeNode* cur = root;
while(cur!=nullptr || !stk.empty()){
if(cur!=nullptr){
stk.push(cur);
cur = cur->left;
}else{
cur = stk.top();
stk.pop();
resu.push_back(cur->val);
cur = cur->right;
}
}
return resu;
}
};
- 后序遍历
- 左-右-中
- 对先序遍历算法略做修改。调换将节点左右孩子入栈时的顺序,变成中-右-左,翻转resu向量得到左-右-中
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> resu;
stack<TreeNode*> stk;
if(root == nullptr) return resu;
stk.push(root);
while(!stk.empty()){
TreeNode* tmp = stk.top();
resu.push_back(tmp->val);
stk.pop();
if(tmp->left) stk.push(tmp->left);
if(tmp->right) stk.push(tmp->right);
}
reverse(resu.begin(), resu.end());
return resu;
}
};
三、二叉树的三种遍历(统一迭代版)
- 思路
- 解决访问节点的顺序与处理节点的顺序不一致问题(中序)
- 解决方法:标记法,即将要处理的节点入栈后在其后入栈一个空指针。在后续的访问中如果当前栈顶为空指针,则处理接下来的节点,将其加入到结果向量中。
- 前序遍历
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> resu;
stack<TreeNode*> stk;
if(root != NULL) stk.push(root);
while(!stk.empty()){
TreeNode* cur = stk.top();
if(cur!=NULL){
stk.pop();
if(cur->right) stk.push(cur->right);
if(cur->left) stk.push(cur->left);
stk.push(cur);
stk.push(NULL);
}else{
stk.pop();
resu.push_back(stk.top()->val);
stk.pop();
}
}
return resu;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> resu;
stack<TreeNode*> stk;
if(root!=NULL) stk.push(root);
while(!stk.empty()){
TreeNode* cur = stk.top();
if(cur!=NULL){
stk.pop();
if(cur->right) stk.push(cur->right);
stk.push(cur);
stk.push(NULL);
if(cur->left) stk.push(cur->left);
}else{
stk.pop();
resu.push_back(stk.top()->val);
stk.pop();
}
}
return resu;
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> resu;
stack<TreeNode*> stk;
if(root !=NULL) stk.push(root);
while(!stk.empty()){
TreeNode *cur = stk.top();
if(cur!=NULL){
stk.pop();
stk.push(cur);
stk.push(NULL);
if(cur->right) stk.push(cur->right);
if(cur->left) stk.push(cur->left);
}else{
stk.pop();
resu.push_back(stk.top()->val);
stk.pop();
}
}
return resu;
}
};
四、二叉树的三种遍历(morris版)
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> resu;
TreeNode *cur = root;
TreeNode *pre;
while(cur){
if(cur->left){
pre = cur->left;
while(pre->right && pre->right!=cur){
pre = pre->right;
}
if(pre->right == NULL){
resu.push_back(cur->val);
pre->right = cur;
cur = cur->left;
}else{
pre->right = NULL;
cur = cur->right;
}
}else{
resu.push_back(cur->val);
cur = cur->right;
}
}
return resu;
}
};
二叉树的遍历迭代版参考详解
morris前序遍历参考详解
|