LeetCode 0106 - Construct Binary Tree from Inorder and Postorder Traversal

2021-08-11 10:52:57 浏览数 (1)

Construct Binary Tree from Inorder and Postorder Traversal

Desicription

Given inorder and postorder traversal of a tree, construct the binary tree.

Note: You may assume that duplicates do not exist in the tree.

Solution

代码语言:javascript复制
/**
 * 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 {
private:
    TreeNode* generateTree(int postStart, int postEnd, int inStart, int inEnd, vector<int>& postorder, vector<int>& inorder) {
        if(postStart > postEnd || inStart > inEnd)
            return NULL;
        TreeNode* root = new TreeNode(postorder[postEnd]);
        int inIndex = 0;
        for(int i = inStart; i <= inEnd; i  ) {
            if(inorder[i] == postorder[postEnd]){
                inIndex = i;
                break;
            }
        }
        root->left = generateTree(postStart, postStart inIndex-inStart-1, inStart, inIndex-1, postorder, inorder);
        root->right = generateTree(postEnd-inEnd inIndex, postEnd-1, inIndex 1, inEnd, postorder, inorder);
        return root;
    }

public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        return generateTree(0, postorder.size()-1, 0, inorder.size()-1, postorder, inorder);
    }
};

0 人点赞