二叉树的的四种遍历先序、中序、后序以及层次遍历
二叉树
每个节点最多有两颗子树,即度 <= 2, 有序树
性质
二叉树的第i层上最多有2^i个节点,i从0开始;
深度为k的二叉树上至多有2^(k+1) - 1个节点,k从0开始;
当前节点编号为i,则其子节点(如果有)为2i+1和2i+2;
完全二叉树
叶子节点只在最大两层出现;
对于任一节点,其右侧分支最大层次为l,则左分支为l或者l+1
满二叉树
深度为k且有2^(k+1) - 1个节点的二叉树
树的结构
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
先序遍历
根据先序遍历规则:根—左—右 分为递归法和使用栈
/**
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
*/
void pre_traversal(TreeNode* root) {
std::stack<TreeNode*> node_stack; //用来暂存节点的栈
while (root != nullptr || !node_stack.empty()) {
if (root != nullptr) { //若当前的节点非空,
std::cout << root->val << " "; //则输出该节点的值
node_stack.push(root); //该节点压入栈中。
root = root->left; // 我们继续向左子树前进
}
else {
root = node_stack.top();
node_stack.pop();
root = root->right;
}
}
}
void PreTraverse(TreeNode* root) {
if (root != NULL)
{
cout << root->val << " ";
PreTraverse(root->left);
PreTraverse(root->right);
}
}
中序遍历
根据先序遍历规则:左—根—右 分为递归法和使用栈
/**
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
*/
void in_traversal(TreeNode* root) {
std::stack<TreeNode*> stack_node;
while (root != nullptr || !stack_node.empty()) {
if (root != nullptr) {
stack_node.push(root);
root = root->left;
}
else {
root = stack_node.top();
std::cout << root->val << " ";
stack_node.pop();
root = root->right;
}
}
}
void InTraverse(TreeNode* root) {
if (root != NULL)
{
InTraverse(root->left);
cout << root->val << " ";
InTraverse(root->right);
}
}
后序遍历
根据先序遍历规则:左—右—根 分为递归法和使用栈
/**
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
*/
void post_traversal(TreeNode* root) {
std::stack<TreeNode*> stack_node;
TreeNode* lastvisit = root;
while (root != nullptr || !stack_node.empty()) {
if (root != nullptr) {
stack_node.push(root);
root = root->left;
}
else {
root = stack_node.top();
if (root->right == nullptr || root->right == lastvisit) {
std::cout << root->val << " ";
stack_node.pop();
lastvisit = root;
root = nullptr;
}
else {
root = root->right;
}
}
}
}
void PostTraverse(TreeNode* root) {
if (root != NULL)
{
PostTraverse(root->left);
PostTraverse(root->right);
cout << root->val << " ";
}
}
层次遍历
根据vector进行输出就得到层次遍历的输出
/**
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
*/
vector<vector<int>> levelOrder(TreeNode* root) {
if (root == NULL) return {};
vector<vector<int>> ans; //设置一个迭代器
queue<TreeNode*> q; //定义一个树形队列q
q.push(root); //将根节点压入队列
while (!q.empty()) {
int count = q.size(); //count为这一层的元素个数
vector<int> level; //level存储这一层中的元素
while (count--) { //这while判断是输出每层的核心level
TreeNode* t = q.front(); //读取队首
level.push_back(t->val); //迭代器尾部插入
q.pop(); //出队,每次只出栈1个数,在用下面if语句向栈中加入他的左右节点
if (t->left) q.push(t->left); //入队
if (t->right) q.push(t->right); //入队
}
ans.push_back(level); //在尾部插入树的每层的值
}
return ans;
}
for循环实现vector<vector>res 容器的输出函数
for(int i=0;i<res.size();i++){
for(int j=0;j<res[i].size();j++){
cout<<res[i][j]<<" ";
}
}