C++ 二叉树的递归遍历【先序,中序,后序】

理论原理:

 代码实现:

#include <iostream>

using namespace std;

struct Binary_Tree_Node
{
    char data;
    Binary_Tree_Node *lchild;
    Binary_Tree_Node *rchild;
    Binary_Tree_Node(char data,Binary_Tree_Node *lchild,Binary_Tree_Node *rchild);
};

Binary_Tree_Node::Binary_Tree_Node(char data,Binary_Tree_Node *lchild,Binary_Tree_Node *rchild)
{
    this->data=data;
    this->lchild=lchild;
    this->rchild=rchild;
}

void traversal(Binary_Tree_Node *root)
{
    //终止条件
    if(root == NULL)
        return;
    //先序遍历
//    cout<<root->data;
//    traversal(root->lchild);
//    traversal(root->rchild);
    //中序遍历
//    traversal(root->lchild);
//    cout<<root->data;
//    traversal(root->rchild);
    //后序遍历
    traversal(root->lchild);
    traversal(root->rchild);
    cout<<root->data;
}

int main()
{
    //创建结点
    Binary_Tree_Node nodeA('A',NULL,NULL);
    Binary_Tree_Node nodeB('B',NULL,NULL);
    Binary_Tree_Node nodeC('C',NULL,NULL);
    Binary_Tree_Node nodeD('D',NULL,NULL);
    Binary_Tree_Node nodeE('E',NULL,NULL);
    Binary_Tree_Node nodeF('F',NULL,NULL);
    Binary_Tree_Node nodeG('G',NULL,NULL);
    Binary_Tree_Node nodeH('H',NULL,NULL);
    //创建树
    nodeA.lchild=&nodeB;
    nodeA.rchild=&nodeF;
    nodeB.rchild=&nodeC;
    nodeC.lchild=&nodeD;
    nodeC.rchild=&nodeE;
    nodeF.rchild=&nodeG;
    nodeG.lchild=&nodeH;
    //遍历二叉树
    traversal(&nodeA);
    return 0;
}

先序遍历运行结果:

 中序遍历运行结果:

后序遍历运行结果: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用C++实现二叉树先序中序后序的代码: ```cpp #include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int> buildTree(vector<int>& preorder, vector<int>& inorder) { vector<int> postorder; if(preorder.empty() || inorder.empty()) { return postorder; } int rootValue = preorder[0]; int inorderIndex = find(inorder.begin(), inorder.end(), rootValue) - inorder.begin(); vector<int> leftPreorder(preorder.begin()+1, preorder.begin()+inorderIndex+1); vector<int> leftInorder(inorder.begin(), inorder.begin()+inorderIndex); vector<int> rightPreorder(preorder.begin()+inorderIndex+1, preorder.end()); vector<int> rightInorder(inorder.begin()+inorderIndex+1, inorder.end()); vector<int> leftPostorder = buildTree(leftPreorder, leftInorder); vector<int> rightPostorder = buildTree(rightPreorder, rightInorder); postorder.insert(postorder.end(), leftPostorder.begin(), leftPostorder.end()); postorder.insert(postorder.end(), rightPostorder.begin(), rightPostorder.end()); postorder.push_back(rootValue); return postorder; } int main() { vector<int> preorder = {1, 2, 4, 5, 3, 6}; vector<int> inorder = {4, 2, 5, 1, 3, 6}; vector<int> postorder = buildTree(preorder, inorder); for(int i=0; i<postorder.size(); i++) { cout << postorder[i] << " "; } cout << endl; return 0; } ``` 在这个程序中,我们首先判断先序遍历中序遍历是否为空,如果为空,则返回空的后序遍历结果。如果不为空,我们取出先序遍历的第一个元素,即为根节点的值。然后在中序遍历中查找根节点的位置,可以使用STL中的find函数来实现。接着,我们将先序遍历中序遍历分别划分为左子树的先序遍历中序遍历,以及右子树的先序遍历中序遍历。然后,分别递归处理左子树和右子树,并将其得到的后序遍历结果添加到最终的后序遍历结果中。最后,将根节点的值添加到后序遍历结果的末尾,并返回后序遍历结果。 希望这个回答对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值