目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、前序遍历
前序遍历可以想象为,一个小人从一棵二叉树根节点为起点,沿着二叉树外沿,逆时针走一圈回到根节点,路上遇到的元素顺序,就是先序遍历的结果
先序遍历结果为:A B D H I E J C F K G
动画演示:
二、中序遍历
中序遍历可以看成,二叉树每个节点,垂直方向投影下来(可以理解为每个节点从最左边开始垂直掉到地上),然后从左往右数,得出的结果便是中序遍历的结果
中遍历结果为:H D I B E J A F K C G
动画展示:
三、后序遍历
后序遍历就像是剪葡萄,我们要把一串葡萄剪成一颗一颗的。
就是围着树的外围绕一圈,如果发现一剪刀就能剪下的葡萄(必须是一颗葡萄)(也就是葡萄要一个一个掉下来,不能一口气掉超过1个这样),就把它剪下来,组成的就是后序遍历了。
后序遍历中,根节点默认最后面
后序遍历结果:H I D J E B K F G C A
动画展示:
四、层次遍历
层次遍历很好理解,就是从根节点开始,一层一层,从上到下,每层从左到右,依次写值就可以了
层次遍历结果:A B C D E F G H I J K
#include<iostream>
#include<queue>
using namespace std;
struct Node
{
char data;
Node* left;
Node* right;
};
void LevelOrder(Node* root)
{
if(root == NULL) return ;
queue<Node*> Q;
Q.push(root);
while(!Q.empty())
{
Node* current = Q.front();
cout<<current->data<<"";
if(current->left != NULL) Q.push(current->left);
if(current->right != NULL) Q.push(current->right);
Q.pop();
}
}
五、代码展示(递归和非递归)
1.递归法:
#include<iostream>
struct Node
{
cahr data;
Node* left;
Node* right;
};
//前序遍历
void Preorder(Node* root)
{
if(root == NULL) return ;
printf("%c ",root->data);
Preorder(root->left);
Preorder(root->right);
}
//中序遍历
void Inorder(Node* root)
{
if(root == NULL) return ;
Inorder(root->left);
printf("%c ",root->data);
Inorder(root->right);
}
//后序遍历
void Postorder(Node* root)
{
if(root == NULL) return ;
Postorder(root->left);
Postorder(root->right);
printf("%c ",root->data);
}
2.非递归法
前序遍历
#include<iostream>
#include<vector>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root)
{
stack<TreeNode*> st;
vector<int> vec;
if(root != nullptr)
{
st.push(root);
}
while(!st.empty())
{
TreeNode* node = st.top();
st.pop();
vec.push_back(node->val);
if(node->right != nullptr)
{
st.push(node->right);
}
if(node->left != nullptr)
{
st.push(node->left);
}
}
return vec;
}
};
中序遍历
#include<iostream>
#include<vector>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root)
{
stack<TreeNode*> st;
vector<int> vec;
TreeNode* node = root;
while(node != nullptr)
{
st.push(node);
node = node->left;
}
while(!st.empty())
{
node = st.top();
vec.push_back(node->val);
st.pop();
node = node->right;
while(node != nullptr)
{
st.push(node);
node = node->left;
}
}
return vec;
}
};
后序遍历
#include<iostream>
#include<vector>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> vec;
TreeNode *node = root, *stTop;
if (root != nullptr) {
st.push(root);
}
while (!st.empty()) {
stTop = st.top();
if (stTop->right == node
|| (stTop->right == nullptr && stTop->left == node)
|| (stTop->left == nullptr && stTop->right == nullptr)) {
st.pop();
node = stTop;
vec.push_back(node->val);
} else {
if (stTop->right != nullptr) {
st.push(stTop->right);
}
if (stTop->left != nullptr) {
st.push(stTop->left);
}
}
}
return vec;
}
};