1--递归遍历
1-1--前序遍历
前序遍历:根→左→右;
#include <iostream>
#include <vector>
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:
std::vector<int> preorderTraversal(TreeNode* root) {
std::vector<int> res;
dfs(root, res);
return res;
}
void dfs(TreeNode* root, std::vector<int>& res){
if(root == nullptr) return;
res.push_back(root->val);
dfs(root->left, res);
dfs(root->right, res);
}
};
int main(int argc, char* argv[]){
// root = [1, null, 2, 3]
TreeNode *Node1 = new TreeNode(1);
TreeNode *Node2 = new TreeNode(2);
TreeNode *Node3 = new TreeNode(3);
Node1->right = Node2;
Node2->left = Node3;
Solution S1;
std::vector<int> res = S1.preorderTraversal(Node1);
for(auto item : res) std::cout << item << " ";
std::cout << std::endl;
return 0;
}
1-2--中序遍历
中序遍历:左→根→右;
#include <iostream>
#include <vector>
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:
std::vector<int> inorderTraversal(TreeNode* root) {
std::vector<int> res;
dfs(root, res);
return res;
}
void dfs(TreeNode* root, std::vector<int>& res){
if(root == nullptr) return;
dfs(root->left, res);
res.push_back(root->val);
dfs(root->right, res);
}
};
int main(int argc, char* argv[]){
// root = [1, null, 2, 3]
TreeNode *Node1 = new TreeNode(1);
TreeNode *Node2 = new TreeNode(2);
TreeNode *Node3 = new TreeNode(3);
Node1->right = Node2;
Node2->left = Node3;
Solution S1;
std::vector<int> res = S1.inorderTraversal(Node1);
for(auto item : res) std::cout << item << " ";
std::cout << std::endl;
return 0;
}
1-3--后序遍历
后序遍历:左→右→根;
#include <iostream>
#include <vector>
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:
std::vector<int> postorderTraversal(TreeNode* root) {
std::vector<int> res;
dfs(root, res);
return res;
}
void dfs(TreeNode* root, std::vector<int>& res){
if(root == nullptr) return;
dfs(root->left, res);
dfs(root->right, res);
res.push_back(root->val);
}
};
int main(int argc, char* argv[]){
// root = [1, null, 2, 3]
TreeNode *Node1 = new TreeNode(1);
TreeNode *Node2 = new TreeNode(2);
TreeNode *Node3 = new TreeNode(3);
Node1->right = Node2;
Node2->left = Node3;
Solution S1;
std::vector<int> res = S1.postorderTraversal(Node1);
for(auto item : res) std::cout << item << " ";
std::cout << std::endl;
return 0;
}
2--迭代遍历
2-1--前序遍历
基于栈结构,先将根节点入栈,再将节点从栈中弹出,如果节点的右孩子不为空,则右孩子入栈;如果节点的左孩子不为空,则左孩子入栈;
循环出栈处理节点,并将右孩子和左孩子存在栈中(右孩子先进栈,左孩子再进栈,因为栈先进后出,这样可以确保左孩子先出栈,符合根→左→右的顺序);
#include <iostream>
#include <vector>
#include <stack>
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:
std::vector<int> preorderTraversal(TreeNode* root) {
std::vector<int> res;
if(root == nullptr) return res;
std::stack<TreeNode*> stk;
stk.push(root);
while(!stk.empty()){
TreeNode *tmp = stk.top();
stk.pop();
res.push_back(tmp->val);
if(tmp->right != nullptr) stk.push(tmp->right); // 右
if(tmp->left != nullptr) stk.push(tmp->left); // 左
}
return res;
}
};
int main(int argc, char* argv[]){
// root = [1, null, 2, 3]
TreeNode *Node1 = new TreeNode(1);
TreeNode *Node2 = new TreeNode(2);
TreeNode *Node3 = new TreeNode(3);
Node1->right = Node2;
Node2->left = Node3;
Solution S1;
std::vector<int> res = S1.preorderTraversal(Node1);
for(auto item : res) std::cout << item << " ";
std::cout << std::endl;
return 0;
}
2-2--后序遍历
可以使用两个栈来实现,一个是遍历栈,一个是收集栈,参考之前的笔记:后序遍历的迭代实现
也可以类似于前序遍历,基于一个栈实现,只不过需要改变入栈顺序:每出栈处理一个节点,其左孩子入栈,再右孩子入栈;此时处理顺序为:根->右->左,最后将结果 reverse 即可;
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
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:
std::vector<int> postorderTraversal(TreeNode* root) {
std::vector<int> res;
if(root == nullptr) return res;
std::stack<TreeNode*> stk;
stk.push(root);
while(!stk.empty()){
TreeNode* tmp = stk.top();
stk.pop();
if(tmp->left != nullptr) stk.push(tmp->left);
if(tmp->right != nullptr) stk.push(tmp->right);
res.push_back(tmp->val);
}
// 反转
std::reverse(res.begin(), res.end());
return res;
}
};
int main(int argc, char* argv[]){
// root = [1, null, 2, 3]
TreeNode *Node1 = new TreeNode(1);
TreeNode *Node2 = new TreeNode(2);
TreeNode *Node3 = new TreeNode(3);
Node1->right = Node2;
Node2->left = Node3;
Solution S1;
std::vector<int> res = S1.postorderTraversal(Node1);
for(auto item : res) std::cout << item << " ";
std::cout << std::endl;
return 0;
}