由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 弱问:不好意思,这个CODE问题在哪里?
相关主题
以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)A onsite被拒,面经,求分析失败原因
刷题弱人来问个two sum的题目有简洁树代码么
Tree Question: Longest path from root to a leaf问个打印树的问题
问个fb onsite题目binary tree的in-order iterator怎么写?
刚才的amazon phone interview 第一轮leetcode 的 Insert Interval 就是过不了大的
题目: iterative binary tree post order traversalLeetcode上Two sum只能过3个case, VS能过,大牛进来看看是怎么回事
谷歌 电面一道关于trie的题目
弱问一个数据结构Leetcode Two Sum,我这个O(n)解法为啥不讨服务器的好呢
相关话题的讨论汇总
话题: btree话题: int话题: num话题: binarytree话题: nums
进入JobHunting版参与讨论
1 (共1页)
h*****i
发帖数: 1017
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]);
}
}
l**g
发帖数: 133
2
插入root的时候,并没有有效的赋值
insert操作通常返回插入节点的位置或iterator
T******e
发帖数: 157
3
看到insert函数,里面用new产生新node,但函数返回后这些指针都没了,一方面导致
你无法再到达那些node,另一方面就是内存泄漏了
b********6
发帖数: 35437
4
void insert(binaryTree *&bTree, int num, int key)
加个&看能不能解决问题
u****0
发帖数: 155
5
new BTree後面的()呢
1 (共1页)
进入JobHunting版参与讨论
相关主题
Leetcode Two Sum,我这个O(n)解法为啥不讨服务器的好呢刚才的amazon phone interview 第一轮
请问下leetcode的two sum题目题目: iterative binary tree post order traversal
BinaryTree to DoublyLinkedList谷歌 电面
共享一道电面题k-sum弱问一个数据结构
以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)A onsite被拒,面经,求分析失败原因
刷题弱人来问个two sum的题目有简洁树代码么
Tree Question: Longest path from root to a leaf问个打印树的问题
问个fb onsite题目binary tree的in-order iterator怎么写?
相关话题的讨论汇总
话题: btree话题: int话题: num话题: binarytree话题: nums