恢复二叉树思路及代码

  • 思想:

        以下图二叉树为例进行思路演示

        先序:(根、左、右)a b d e c f g

        中序:(左、根、右)d b e a f c g

        后序:(左、右、根)d e b f g c a

  •         先序+中序恢复

                        1)前序第一个为根a,找中序内和先序第一个相等的根位置

                        

                         2)中序遍历将左右子树隔开,左子树个数为3。先序遍历中,从根a后面找3个

                         3)递归划分左子树: 先序遍历可知b是左子树的根,从中序遍历中找到根b,b左边是左子树,右边是右子树

                        重复3)步骤,找到右子树的根c,在中序遍历中将c的左子树f,右子树g找到。

  •         中序+后序恢复

                        1)后序中最后一个字符a为根,在中序中找到根a的位置

                         2)在中序中根a的左边为左子树,右边为右子树

                        3) 再从后序中找到左子树根b,右子树的根c,然后在中序中根左边为左子树,右边为右子树。递归重复进行查找。

  • 代码:
typedef struct node
{
    char value;//当前节点值
    struct node* left;//指向当前节点左孩子指针
    struct node* right;//指向当前节点右孩子指针
}Node;

typedef struct tree
{
    Node* root;
}Tree;

void InitTree(Tree* t)
{
    t->root = NULL;
}


//用先序字符串和中序字符串恢复二叉树
Node* Create(const char* vlr,const char* lvr,int n)
{//函数参数:vlr: 先序遍历,lvr: 中序遍历n: 当前字符串个数
    //字符串遍历完毕,即为到达叶子节点,无左右子树
    if (n == 0)
        return NULL;
    //还有字符串
    else
    {
        int k = 0;
        while (vlr[0] != lvr[k])
        {
            k++;
        }
        Node* root = (Node*)malloc(sizeof(Node));
        root->value = vlr[0];
        //左子树
        root->left = Create(vlr + 1, lvr, k);
        //右子树
        root->right = Create(vlr + 1+k, lvr+k+1, n-k-1);
        return root;
    }
}

//后序
void PostOrder(node* root)
{
    if (root != NULL)
    {
        PostOrder(root->left);
        PostOrder(root->right);
        printf("%c", root->value);
    }
}

int main()
{
    const char* vlr = "abdecfg";//先序
    const char* lvr = "dbeafcg";//中序
    Tree t;
    InitTree(&t);
    int n = strlen(vlr);
    t.root = Create(vlr, lvr, n);
    PostOrder(t.root);//后序遍历输出验证一下
    printf("\n");
}

点击此处参考后序遍历代码

  • 运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
叉树的创建及应用代码可以分为两部分:节点定义和二叉树的创建及应用。 节点定义: ```c++ struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; ``` 二叉树的创建及应用: ```c++ #include <iostream> #include <queue> using namespace std; // 创建二叉树 TreeNode* createTree() { int val; cin >> val; if (val == -1) { return NULL; } TreeNode* root = new TreeNode(val); root->left = createTree(); root->right = createTree(); return root; } // 先序遍历 void preOrder(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } // 中序遍历 void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } // 后序遍历 void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } // 层序遍历 void levelOrder(TreeNode* root) { queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* cur = q.front(); q.pop(); cout << cur->val << " "; if (cur->left != NULL) { q.push(cur->left); } if (cur->right != NULL) { q.push(cur->right); } } } int main() { TreeNode* root = createTree(); cout << "先序遍历:"; preOrder(root); cout << endl; cout << "中序遍历:"; inOrder(root); cout << endl; cout << "后序遍历:"; postOrder(root); cout << endl; cout << "层序遍历:"; levelOrder(root); cout << endl; return 0; } ``` 以上代码实现了二叉树的创建和四种遍历方式(先序、中序、后序、层序遍历)。其中,createTree()函数通过递归的方式创建二叉树。主函数中,先创建二叉树,然后分别进行各种遍历。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值