Redian新闻
>
弱问:不好意思,这个CODE问题在哪里?
avatar
弱问:不好意思,这个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]);
}
}
avatar
l*g
2
插入root的时候,并没有有效的赋值
insert操作通常返回插入节点的位置或iterator
avatar
T*e
3
看到insert函数,里面用new产生新node,但函数返回后这些指针都没了,一方面导致
你无法再到达那些node,另一方面就是内存泄漏了
avatar
b*6
4
void insert(binaryTree *&bTree, int num, int key)
加个&看能不能解决问题
avatar
u*0
5
new BTree後面的()呢
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。