leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binar

Posted jzssuanfa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binar相关的知识,希望对你有一定的参考价值。

1、


Construct Binary Tree from Inorder and Postorder Traversal

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

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

代码:

class Solution {
public:
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        TreeNode* root = NULL;
        int len1 = inorder.size();
        int len2 = postorder.size();
        if(len1<1 || len2<1 || len1!=len2){
            return root;
        }
        return buildTreeCore(inorder,0,len1-1,postorder,0,len2-1);
    }
    TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&postorder, int startIndex2, int endIndex2){
        if(endIndex1 < startIndex1  ){
            return NULL;
        }
        TreeNode* root = new TreeNode(postorder[endIndex2]);
        int index = 0;
        for(int i=startIndex1; i<=endIndex1; ++i){
            if(inorder[i] == postorder[endIndex2]){
                index = i;
                break;
            }
        }
        int leftLen = index-startIndex1;
        TreeNode* leftNode = NULL;
        TreeNode* rightNode = NULL;
       
        leftNode = buildTreeCore(inorder,startIndex1,index-1,postorder,startIndex2,startIndex2+leftLen-1);
        rightNode = buildTreeCore(inorder,index+1,endIndex1,postorder,startIndex2+leftLen,endIndex2-1);
      
        root->left = leftNode;
        root->right = rightNode;
        return root;
    }
};

2、Construct Binary Tree from Preorder and Inorder Traversal 

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

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

class Solution {
public:
    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        TreeNode* root = NULL;
        int len1 = inorder.size();
        int len2 = preorder.size();
        if(len1<1 || len2<1 || len1!=len2){
            return root;
        }
        return buildTreeCore(inorder,0,len1-1,preorder,0,len2-1);
    }
      TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&preorder, int startIndex2, int endIndex2){
        if(endIndex1 < startIndex1  ){
            return NULL;
        }
        TreeNode* root = new TreeNode(preorder[startIndex2]);
        int index = 0;
        for(int i=startIndex1; i<=endIndex1; ++i){
            if(inorder[i] == preorder[startIndex2]){
                index = i;
                break;
            }
        }
        int leftLen = index-startIndex1;
        TreeNode* leftNode = NULL;
        TreeNode* rightNode = NULL;
       
        leftNode = buildTreeCore(inorder,startIndex1,index-1,preorder,startIndex2+1,startIndex2+leftLen);
        rightNode = buildTreeCore(inorder,index+1,endIndex1,preorder,startIndex2+leftLen+1,endIndex2);
      
        root->left = leftNode;
        root->right = rightNode;
        return root;
    }
};






以上是关于leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binar的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode&Python] Problem 492. Construct the Rectangle

[LeetCode&Python] Problem 427. Construct Quad Tree

LeetCode Construct String from Binary Tree

LeetCode:492. Construct the Rectangle

492. Construct the Rectangle(LeetCode)

leetcode-492-Construct the Rectangle