弱问:不好意思,这个CODE问题在哪里?# JobHunting - 待字闺中
h*i
1 楼
#include
#include
using namespace std;
class binaryTree {
public:
int value;
int indx;
binaryTree *left;
binaryTree *right;
binaryTree(){
left = NULL;
right = NULL;
}
};
void insert(binaryTree *bTree, int num, int key){
if(bTree == NULL){
bTree = new binaryTree;
bTree->value = num;
bTree->indx = key;
printf("%d %dn",bTree->value,bTree->indx);
return;
}
if(num >= bTree->value){
printf("Insert to the right %dn",num);
insert(bTree->right,num,key);
} else {
insert(bTree->left,num,key);
printf("Insert to the left %dn",num);
}
}
int search(binaryTree *bTree, int num) {
if (bTree == NULL) {
return -1;
}
if (num == bTree->value) {
return bTree->indx;
}
else if (num > bTree->value) {
return search(bTree->right, num);
}
else {
return search(bTree->left, num);
}
}
class Solution {
public:
vector twoSum(vector& nums, int target) {
vector result;
//Construct binary tree
binaryTree *bTree;
bTree = NULL;
for (int i = 0; i < nums.size(); i ++){
insert(bTree,nums[i],i);
printf("Out %dn",bTree->value);
}
//Search
for (int i = 0; i < nums.size(); i ++){
int match = search(bTree,target - nums[i]);
if(match >=0) {
result.push_back(i);
result.push_back(match);
}
}
return result;
}
};
int main()
{
Solution sol;
vector nums;
vector res;
nums[0] = 3;
nums[1] = 2;
nums[2] = 4;
res = sol.twoSum(nums, 6);
for (int i = 0; i < res.size(); i++) {
printf("%dn", res[i]);
}
}
#include
using namespace std;
class binaryTree {
public:
int value;
int indx;
binaryTree *left;
binaryTree *right;
binaryTree(){
left = NULL;
right = NULL;
}
};
void insert(binaryTree *bTree, int num, int key){
if(bTree == NULL){
bTree = new binaryTree;
bTree->value = num;
bTree->indx = key;
printf("%d %dn",bTree->value,bTree->indx);
return;
}
if(num >= bTree->value){
printf("Insert to the right %dn",num);
insert(bTree->right,num,key);
} else {
insert(bTree->left,num,key);
printf("Insert to the left %dn",num);
}
}
int search(binaryTree *bTree, int num) {
if (bTree == NULL) {
return -1;
}
if (num == bTree->value) {
return bTree->indx;
}
else if (num > bTree->value) {
return search(bTree->right, num);
}
else {
return search(bTree->left, num);
}
}
class Solution {
public:
vector
vector
//Construct binary tree
binaryTree *bTree;
bTree = NULL;
for (int i = 0; i < nums.size(); i ++){
insert(bTree,nums[i],i);
printf("Out %dn",bTree->value);
}
//Search
for (int i = 0; i < nums.size(); i ++){
int match = search(bTree,target - nums[i]);
if(match >=0) {
result.push_back(i);
result.push_back(match);
}
}
return result;
}
};
int main()
{
Solution sol;
vector
vector
nums[0] = 3;
nums[1] = 2;
nums[2] = 4;
res = sol.twoSum(nums, 6);
for (int i = 0; i < res.size(); i++) {
printf("%dn", res[i]);
}
}