# leetcode-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.

/**
* Definition for binary tree
* 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) {
if (inorder.empty())
return NULL;
return buildTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
}
private:
TreeNode *buildTree(vector<int> &inorder, int istart, int iend,
vector<int> &postorder, int pstart, int pend) {
if (istart == iend)
return new TreeNode(inorder[istart]);

int i;
for (i=istart; i<=iend; ++i) {
if (inorder[i]==postorder[pend]) {
break;
}
}

TreeNode *root = new TreeNode(inorder[i]);

if (i>istart)
root->left = buildTree(inorder, istart, i-1, postorder, pstart, pstart+i-istart-1);

if (i<iend)
root->right = buildTree(inorder, i+1, iend, postorder, pstart+i-istart, pend-1);

return root;
}

};

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

## 【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: Construct Binary Tree from Inorder and Postorder Traversal

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

## [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】Construct Binary Tree from （Preorder or Postorder） and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal

## 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...
• MebiuW
• 2016年09月16日 16:42
• 313

## 【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal

【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. 【思路】 Hint: A...

## 【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-Construct Binary Tree from Inorder and Postorder Traversal 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)