# [leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal

/**
* 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, vector<int>& postorder)
{
int len=inorder.size();
if(len==0)
return {};
else
return build(inorder,postorder,0,len-1,0,len-1);
}
TreeNode* build(vector<int>& inorder, vector<int>& postorder,int inL,int inR, int postL,int postR)
{
if(inR<inL||postR<postL)
return {};

TreeNode* root=new TreeNode(postorder[postR]);

if(inL==inR||postL==postR)
return root;

int mid;
for(int i=inL;i<=inR;i++)
{
if(inorder[i]==postorder[postR])
{
mid=i;
break;
}
}

root->left=build(inorder,postorder,inL,mid-1, postL,postL+mid-inL-1);
root->right=build(inorder,postorder,mid+1,inR,postL+mid-inL,postR-1);

return root;
}
};

• 本文已收录于以下专栏：

## 【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal（构造二叉树II）】

【106-Construct Binary Tree from Inorder and Postorder Traversal（通过中序和后序遍历构造二叉树II）】【LeetCode-面试算法经典-J...

## 【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume t...

## [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume tha...

## LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers...

## Leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume tha...

## Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树

1 解题思想这道题和105的思想，基本就是一致的，这里将会说的比较简单，之说下不同 Leetcode 105. Construct Binary Tree from Preorder and Ino...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)