diff --git a/C++/001_Two_Sum.cpp b/C++/001_Two_Sum.cpp deleted file mode 100644 index 40f0e1af..00000000 --- a/C++/001_Two_Sum.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - *Given an array of integers, return indices of the two numbers such that they add up to a specific target. - *You may assume that each input would have exactly one solution. - - *Example: - *Given nums = [2, 7, 11, 15], target = 9, - *Because nums[0] + nums[1] = 2 + 7 = 9, - *return [0, 1]. - - *UPDATE (2016/2/13): - *The return format had been changed to zero-based indices. Please read the above updated description carefully - - *Tag: Array, Hash Table - - *Author: Linsen Wu -*/ - -#include "stdafx.h" -#include "iostream" -#include -#include - -using namespace std; - -class Solution { -public: - vector twoSum(vector &numbers, int target) { - vector indexResults; - unordered_map index_map; - unordered_map::iterator it; - for (int i=0; isecond); - indexResults.push_back(i); - return indexResults; - } else { - index_map[numbers[i]] = i; - } - } - return indexResults; - } -}; - -/* -Save the numbers into an unordered map when searching -Time: O(n) -Space: O(n) -*/ - -int _tmain(int argc, _TCHAR* argv[]) -{ - vector input, output; - input.push_back(-1); - input.push_back(7); - input.push_back(11); - input.push_back(15); - - Solution _solution; - output = _solution.twoSum(input, 6); - - for(vector::iterator iterator = output.begin(); iterator != output.end(); iterator++) - cout<<((*iterator))< +#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 { +public: + TreeNode* buildTree(vector& preorder, vector& inorder) { + return subtree(preorder, 0, preorder.size() - 1 , inorder, 0, preorder.size() - 1); + } + TreeNode* subtree(vector& preorder, int begin1, int end1, vector& inorder, int begin2, int end2){ + int i; + if(end1 < begin1) + return NULL; + if(end1 == begin1) + return new TreeNode(preorder.at(begin1)); + + TreeNode* root = new TreeNode(preorder.at(begin1)); + for(i = begin2; i < end2; i++){ + if(inorder.at(i) == root->val) + break; + } + int left_length = i - begin2; + root->left = subtree(preorder, begin1 + 1, begin1 + left_length, inorder, begin2, begin2 + left_length - 1); + root->right = subtree(preorder, begin1 + left_length + 1, end1, inorder, begin2 + left_length + 1, end2); + return root; + } +}; + +void main(){ + + int pre[] = {1,2,3,4}; + vector preorder(pre, pre + sizeof(pre) / sizeof(int)); + int in[] = {2,1,4,3}; + vector inorder(in, in + sizeof(in) / sizeof(int)); + Solution sol; + TreeNode* root = sol.buildTree(preorder, inorder); + +} diff --git a/C++/124_Binary_Tree_Maximum_Path_Sum.cpp b/C++/124_Binary_Tree_Maximum_Path_Sum.cpp deleted file mode 100644 index 5fa49b26..00000000 --- a/C++/124_Binary_Tree_Maximum_Path_Sum.cpp +++ /dev/null @@ -1,68 +0,0 @@ -// 124. Binary Tree Maximum Path Sum -/** - * Given a binary tree, find the maximum path sum. - * - * For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. - * - * For example: - * Given the below binary tree, - * - * 1 - * / \ - * 2 3 - * Return 6. - * - * Tags: Tree, Depth-first Search - * - * Author: Kuang Qin - */ - -#include "stdafx.h" -#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 { - int curMax; - -public: - int maxPathSum(TreeNode* root) { - curMax = INT_MIN; - dfs(root); - return curMax; - } - -// if we look at each individual nodes, there are only 3 possiblities: -// a) Left branch: root->val + left -// b) Right branch: root->val + right -// c) Both branches: root->val + left + right -// -// a) or b): it can be accumulated between different nodes, so we use them as the return value in the recursion -// c): it can be only a local maximum, so we compare it to the global maximum and keep whichever is larger - - int dfs(TreeNode* root) { - if (!root) return 0; - - int left = max(dfs(root->left), 0); // if left child val < 0, keep only the parent node value - int right = max(dfs(root->right), 0); // if right child val < 0, keep only the parent node value - curMax = max(curMax, root->val + left + right); // compare the global max to possible local max - - return root->val + max(left, right); // choose whichever is larger - } -}; - -int _tmain(int argc, _TCHAR* argv[]) -{ - return 0; -} -