Construct Binary Tree from Inorder and Postorder Traversal Solutions in C++
Number 106
Difficulty Medium
Acceptance 47.3%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/// Author : Hao Chen// Date : 2014-07-10#include <stdio.h>#include <vector>#include <queue>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};TreeNode *buildTree(vector<int> &inorder, int in_offset, vector<int> &postorder, int post_offset, int n );TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {return buildTree(inorder, 0, postorder, 0, postorder.size());}// n - how many number, offset - start from where?TreeNode *buildTree(vector<int> &inorder, int in_offset, vector<int> &postorder, int post_offset, int n ) {if ( n<=0 || postorder.size()<=0 || inorder.size()<=0 ) return NULL;TreeNode *root = new TreeNode(postorder[post_offset+n-1]);if ( n==1 ){return root;}//searching in inorder -- can be optimized by using <map>int i;for(i=in_offset; i<in_offset+n; i++){if (inorder[i] == postorder[post_offset+n-1]){break;}}//error: not foundif (i == inorder.size()) return NULL;int left_n = i - in_offset;int right_n = in_offset + n - i - 1;root->left = buildTree(inorder, in_offset, postorder, post_offset, left_n );root->right = buildTree(inorder, i+1, postorder, post_offset+left_n, right_n);return root;}//cause the problem: memory limited errorTreeNode *buildTree2(vector<int> &inorder, vector<int> &postorder) {if (postorder.size()<=0 || inorder.size()<=0 ) return NULL;int post_n = postorder.size();TreeNode *root = new TreeNode(postorder[post_n-1]);if ( inorder.size()==1 && postorder.size()==1 ){return root;}//searching in inorder -- can be optimized by using <map>int i;for(i=0; i<inorder.size(); i++){if (inorder[i] == postorder[post_n-1]){break;}}//error: not foundif (i == inorder.size()) return NULL;vector<int> in(inorder.begin(), inorder.begin()+i);vector<int> post(postorder.begin(), postorder.begin()+i);if (in.size()>0) {root->left = buildTree(in, post);}in.clear();in.assign(inorder.begin()+i+1, inorder.end());post.clear();post.assign(postorder.begin()+i, postorder.end()-1);if (in.size()>0) {root->right = buildTree(in, post);}return root;}void printTree_pre_order(TreeNode *root){if (root == NULL){printf("# ");return;}printf("%c ", root->val );printTree_pre_order(root->left);printTree_pre_order(root->right);}void printTree_in_order(TreeNode *root){if (root == NULL){printf("# ");return;}printTree_in_order(root->left);printf("%c ", root->val );printTree_in_order(root->right);}void printTree_level_order(TreeNode *root){queue<TreeNode*> q;q.push(root);while (q.size()>0){TreeNode* n = q.front();q.pop();if (n==NULL){printf("# ");continue;}printf("%c ", n->val);q.push(n->left);q.push(n->right);}printf("\n");}int main(){int in_order[]={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};int post_order[]={'A', 'C', 'E', 'D', 'B', 'H', 'I', 'G', 'F'};vector<int> inorder( in_order, in_order + 9 );vector<int> postorder( post_order, post_order + 9 );TreeNode* tree = buildTree(inorder, postorder);printTree_level_order(tree);printTree_pre_order(tree);printf("\n");printTree_in_order(tree);printf("\n");return 0;}
C++ solution by pezy/LeetCode
#include <cstddef>#include <vector>#include <algorithm>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:using vcIt = vector<int>::const_iterator;TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {return buildTree(inorder.cbegin(), inorder.cend(), postorder.cbegin(), postorder.cend());}TreeNode *buildTree(vcIt inBeg, vcIt inEnd, vcIt postBeg, vcIt postEnd) {if (inBeg >= inEnd || postBeg >= postEnd) return NULL;TreeNode *root = new TreeNode(*prev(postEnd));auto inRoot = find(inBeg, inEnd, root->val);root->left = buildTree(inBeg, inRoot, postBeg, next(postBeg, inRoot-inBeg));root->right = buildTree(next(inRoot), inEnd, next(postBeg, inRoot-inBeg), prev(postEnd));return root;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-06-03#include <iostream>#include <vector>#include <cassert>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Recursive/// Time Complexity: O(n*h) where n is the num of node in th tree/// and h is the height of the tree/// Space Complexity: O(h)class Solution {public:TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {return buildTree(inorder, 0, inorder.size(), postorder, 0, postorder.size());}private:TreeNode* buildTree(vector<int>& inorder, int inorderL, int inorderR,vector<int>& postorder, int postorderL, int postorderR){if(inorderL >= inorderR){assert(postorderL >= postorderR);return NULL;}if(inorderL + 1 == inorderR){assert(postorderL + 1 == postorderR);return new TreeNode(inorder[inorderL]);}TreeNode* root = new TreeNode(postorder[postorderR - 1]);int rootPos = find(inorder.begin() + inorderL, inorder.begin() + inorderR, root->val) - inorder.begin();assert(inorderL <= rootPos && rootPos < inorderR);int lsize = rootPos - inorderL;int rsize = inorderR - (rootPos + 1);root->left = buildTree(inorder, inorderL, inorderL + lsize, postorder, postorderL, postorderL + lsize);root->right = buildTree(inorder, rootPos + 1, inorderR, postorder, postorderL + lsize, postorderR - 1);return root;}};int main() {vector<int> inorder = {9,3,15,20,7};vector<int> postorder = {9,15,7,20,3};TreeNode* root = Solution().buildTree(inorder, postorder);return 0;}