C++ 二叉树由先序(后序)和中序遍历遍历建立二叉树

分为对应Leetcode 105和106

根据一棵树的前序遍历与中序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]

返回如下的二叉树:

    3
   / \
  9  20
    /  \
   15   7

算法是使用辅助递归函数,辅助函数需要记录前序数组的起点和中点,数组均使用前闭后闭约定。

中序左子数的区间为[inStart,inRoot-1],中序右子数的区间为[inRoot+1,inEnd]

前序左子数的区间为[preStart+1,preStart+(inRoot-1-inStart)+1]      // 由前序左子数的个数等于中序左子树计算得到

前序右子数的区间为[preStart+(inRoot-inStart)+1,preEnd],

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, int preStart,int preEnd, vector<int>& inorder,int inStart, int inEnd){
        if(preStart>preEnd)
            return NULL;
        
        int rootVal = preorder[preStart];
        TreeNode* root = new TreeNode(rootVal);
        int inRoot = inStart;
        while(inRoot<inEnd && inorder[inRoot]!=rootVal)
            inRoot++;
        
        root->left = buildTree(preorder, preStart+1,preStart+inRoot-inStart,inorder,inStart,inRoot-1);
        root->right = buildTree(preorder,preStart+inRoot-inStart+1,preEnd,inorder,inRoot+1,inEnd);
        return root;
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return buildTree(preorder,0,preorder.size()-1, inorder,0,inorder.size()-1);
    }
};

对于后序遍历

中序左子数的区间为[inStart,inRoot-1],中序右子数的区间为[inRoot+1,inEnd]

后序左子数的区间为[posStart,posStart+inRoot-1-inStart],

后序右子数的区间为[posStart+inRoot-inStart,posEnd-1]

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, int inStart, int inEnd, vector<int> postorder, int posStart, int posEnd){
        if(inStart>inEnd)
            return NULL;
        
        int rootVal = postorder[posEnd];
        TreeNode* root = new TreeNode(rootVal);
        int inRoot = inStart;
        while(inRoot<inEnd && inorder[inRoot]!=rootVal)
            inRoot++;
        
        root->left = buildTree(inorder,inStart,inRoot-1,postorder,posStart, posStart+inRoot-1-inStart);
        root->right = buildTree(inorder,inRoot+1,inEnd,postorder, posStart+inRoot-inStart,posEnd-1);
        return root;
        
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        return buildTree(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);
    }
};

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用C语言实现二叉树先序遍历中序遍历后序遍历的代码: ``` #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 创建一个新的节点 TreeNode* createNode(int val) { TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } // 向二叉树中插入一个节点 TreeNode* insert(TreeNode *root, int val) { if (root == NULL) { return createNode(val); } if (val < root->val) { root->left = insert(root->left, val); } else if (val > root->val) { root->right = insert(root->right, val); } return root; } // 先序遍历 void preorder(TreeNode *root) { if (root != NULL) { printf("%d ", root->val); preorder(root->left); preorder(root->right); } } // 中序遍历 void inorder(TreeNode *root) { if (root != NULL) { inorder(root->left); printf("%d ", root->val); inorder(root->right); } } // 后序遍历 void postorder(TreeNode *root) { if (root != NULL) { postorder(root->left); postorder(root->right); printf("%d ", root->val); } } int main() { TreeNode *root = createNode(5); insert(root, 2); insert(root, 8); insert(root, 1); insert(root, 3); insert(root, 6); insert(root, 9); printf("先序遍历:"); preorder(root); printf("\n"); printf("中序遍历:"); inorder(root); printf("\n"); printf("后序遍历:"); postorder(root); printf("\n"); return 0; } ``` 以上代码运行结果为: ``` 先序遍历:5 2 1 3 8 6 9 中序遍历:1 2 3 5 6 8 9 后序遍历:1 3 2 6 9 8 5 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值