From 5b4cca3e267aaa170c60b3a7a78c62057abc2e96 Mon Sep 17 00:00:00 2001 From: Kuang Qin Date: Tue, 4 Apr 2017 10:43:55 -0500 Subject: [PATCH] Add Solution for 105 Construct Binary Tree from Preorder and Inorder Traversal --- ...ee_from_Preorder_and_Inorder_Traversal.cpp | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 cpp/105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.cpp diff --git a/cpp/105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.cpp b/cpp/105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.cpp new file mode 100644 index 0000000..73a8916 --- /dev/null +++ b/cpp/105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.cpp @@ -0,0 +1,172 @@ +// 105. 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. + * + * Tags: Tree, Array, Depth-first Search + * + * Similar Problems: (M) Construct Binary Tree from Inorder and Postorder Traversal + * + * Author: Kuang Qin + */ + +#include +#include +#include +#include + +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) {} +}; + +class Solution { + TreeNode* build(vector& preorder, int proot, vector& inorder, int istart, int iend) { + if (proot > preorder.size() - 1 || istart > iend) { + return NULL; + } + + TreeNode *root = new TreeNode(preorder[proot]); + + int iroot; + for (iroot = istart; iroot < iend; iroot++) { + if (inorder[iroot] == preorder[proot]) { + break; + } + } + + root->left = build(preorder, proot + 1, inorder, istart, iroot - 1); + root->right = build(preorder, proot + (iroot - istart) + 1, inorder, iroot + 1, iend); + return root; + } +public: + TreeNode* buildTree(vector& preorder, vector& inorder) { + int n = preorder.size(); + if (n == 0) { + return NULL; + } + + return build(preorder, 0, inorder, 0, n - 1); + } +}; + +class TreeOperation { + int getTreeHeight(TreeNode* root) { + if (root == NULL) { + return 0; + } + + int l = getTreeHeight(root->left); + int r = getTreeHeight(root->right); + + if (l > r) { + return l + 1; + } + + return r + 1; + } + + // output all the node in level order, including null pointers + vector> levelOrderFull(TreeNode* root) { + vector> output; + if (root == NULL) { + return output; + } + + int h = getTreeHeight(root); + queue q; + q.push(root); + + // fill the container in each level + for (int i = 0; i < h; i++) { + vector level; + int currLevelCount = q.size(); + + // while loop for current level + while (currLevelCount--) { + TreeNode *curr = q.front(); + if (curr == NULL) { + level.push_back("&"); + q.push(NULL); + q.push(NULL); + } + else { + stringstream ss; + ss << curr->val; + level.push_back(ss.str()); + q.push(curr->left); + q.push(curr->right); + } + + q.pop(); + } + + output.push_back(level); + } + + return output; + } +public: + void printTree(TreeNode *root) { + vector> output = levelOrderFull(root); + int h = output.size(); + if (h == 0) { + return; + } + + int w = 2 * output[h - 1].size() + 1; // total width + for (int i = 0; i < h; i++) { + int n = output[i].size(); + int m = (w - n) / (n + 1); // calculate space width + string sp(m, ' '); + if ((w - n) % (n + 1)) { + cout << sp << " "; + } + else { + cout << sp; // add space + } + + for (int j = 0; j < n; j++) { + cout << output[i][j] << sp; + } + cout << endl; + } + + return; + } + + void deleteTree(TreeNode *root) { + if (root == NULL) { + return; + } + + if (root->left == NULL && root->right == NULL) { + delete root; + return; + } + + deleteTree(root->left); + deleteTree(root->right); + return; + } +}; + +int main() { + vector preorder = {1,2,4,3,5,6}; + vector inorder = {4,2,1,5,3,6}; + Solution sol; + TreeNode *root = sol.buildTree(preorder, inorder); + TreeOperation trOp; + trOp.printTree(root); + trOp.deleteTree(root); + return 0; +} \ No newline at end of file