算法入门(四):二叉树 - 递归遍历三件套
前、中、后序遍历
本文暂不介绍二叉树的定义以及深度等概念。
这里给出一棵深度为3的二叉树:
cpp
A
/ \
B C
/ \ / \
D E F G
其三种遍历结果如下:
-
前序遍历(根 → 左 → 右)
顺序:A → B → D → E → C → F → G
-
中序遍历(左 → 根 → 右)
顺序:D → B → E → A → F → C → G
-
后序遍历(左 → 右 → 根)
顺序:D → E → B → F → G → C → A

递归
递归需要思考清楚以下问题:
- 递归函数的功能是什么,输入输出分别是什么
- 递归函数的终止条件是什么,什么时候结束或者停止
- 递推式是什么
对于二叉树的遍历问题:
- 遍历函数的输入是根节点,输出是将节点值放入结果数组:res.push_back(node->val)
- 当节点为空,结束遍历函数
- 遍历函数的递推式就是遍历的动作,区分为前中后序
LC 144(前序)
第一次尝试:
cpp
class Solution {
public:
vector<int> res;
void traversal(TreeNode* node){
if(node == nullptr) return;
res.push_back(node->val);
traversal(node->left);
traversal(node->right);
}
vector<int> preorderTraversal(TreeNode* root) {
traversal(root);
return res;
}
};
如果想要让res不是全局变量的话,可以通过&传值,在vector vec中添加 &:
cpp
class Solution {
public:
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == nullptr) return;
vec.push_back(cur->val); // 中
traversal(cur->left, vec); // 左
traversal(cur->right, vec); // 右
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root,res);
return res;
}
};
LC 94(中序)
同上。修改traversal中的顺序,中序遍历代表push动作在中间:
cpp
class Solution {
public:
void traversal(TreeNode* cur,vector<int>& vec){
if(cur ==nullptr) return ;
traversal(cur->left,vec);
vec.push_back(cur->val);
traversal(cur->right,vec);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root,res);
return res;
}
};
LC 145(后序)
后序遍历代表push动作在最后:
cpp
class Solution {
public:
void traversal(TreeNode* cur,vector<int>& vec){
if(cur == nullptr) return;
traversal(cur->left,vec);
traversal(cur->right,vec);
vec.push_back(cur->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root,res);
return res;
}
};
LC 589(N叉树前序)
如果前三道题掌握的话,这道题不会很困难:
cpp
class Solution {
public:
void traversal(Node* node,vector<int>& res){
if(node == nullptr) return;
res.push_back(node->val);
if(node->children.size() == 0) return;
for(int i =0;i<node->children.size();i++){
traversal(node->children[i],res);
}
}
vector<int> preorder(Node* root) {
vector<int> res;
traversal(root,res);
return res;
}
};