Construct Binary Tree from Preorder and Inorder Traversal Solutions in C++
Number 105
Difficulty Medium
Acceptance 49.0%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/// Author : Hao Chen// Date : 2014-07-09#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>& preorder, int& preidx, vector<int>& inorder);TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {int preidx=0;return buildTree(preorder, preidx, inorder);}TreeNode *buildTree(vector<int>& preorder, int& preidx, vector<int>& inorder) {if (preorder.size()<=0 || inorder.size()<=0 ) return NULL;TreeNode *root = new TreeNode(preorder[preidx]);if (inorder.size()==1){return root;}int i;for(i=0; i<inorder.size(); i++){if (inorder[i] == preorder[preidx]){break;}}//error: not foundif (i == inorder.size()) return NULL;if (preidx+1 >= preorder.size()){return root;}vector<int> v(inorder.begin(), inorder.begin()+i);if (v.size()>0) {preidx++;root->left = buildTree(preorder, preidx, v);}v.clear();v.assign(inorder.begin()+i+1, inorder.end());if (v.size()>0) {preidx++;root->right = buildTree(preorder, preidx, v);}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 pre_order[]={'F', 'B', 'A', 'D', 'C', 'E', 'G', 'I', 'H'};int in_order[]={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};vector<int> preorder( pre_order, pre_order + 9 );vector<int> inorder( in_order, in_order + 9 );TreeNode* tree = buildTree(preorder, inorder);printTree_level_order(tree);printTree_pre_order(tree);printf("\n");printTree_in_order(tree);printf("\n");return 0;}
C++ solution by pezy/LeetCode
#include <vector>#include <cstddef>#include <algorithm>using std::vector; using std::next; using std::prev;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:using cIter = vector<int>::const_iterator;TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {return buildTree(preorder.cbegin(), preorder.cend(), inorder.cbegin(), inorder.cend());}TreeNode *buildTree(cIter preBeg, cIter preEnd, cIter inBeg, cIter inEnd) {if (preBeg >= preEnd || inBeg >= inEnd) return NULL;TreeNode *root = new TreeNode(*preBeg);auto inRoot = std::find(inBeg, inEnd, root->val);root->left = buildTree(next(preBeg), next(preBeg, inRoot-inBeg+1), inBeg, inRoot);root->right = buildTree(next(preBeg, inRoot-inBeg+1), preEnd, next(inRoot), inEnd);return root;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-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>& preorder, vector<int>& inorder) {return buildTree(preorder, 0, preorder.size(), inorder, 0, inorder.size());}private:TreeNode* buildTree(const vector<int>& preorder, int preorderL, int preorderR,const vector<int>& inorder, int inorderL, int inorderR){if(inorderL >= inorderR){assert(preorderL >= preorderR);return NULL;}if(inorderL + 1 == inorderR){assert(preorderL + 1 == preorderR);return new TreeNode(inorder[inorderL]);}TreeNode* root = new TreeNode(preorder[preorderL]);int rootPos = find(inorder.begin() + inorderL, inorder.begin() + inorderR, root->val) - inorder.begin();assert(rootPos >= inorderL && rootPos < inorderR);int lsize = rootPos - inorderL;int rsize = inorderR - (rootPos + 1);root->left = buildTree(preorder, preorderL + 1, preorderL + 1 + lsize, inorder, inorderL, rootPos);root->right = buildTree(preorder, preorderL + 1 + lsize, preorderR, inorder, rootPos + 1, inorderR);return root;}};int main() {return 0;}