中级农民
- 积分
- 124
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-8-1
- 最后登录
- 1970-1-1
|
Phone Interview。遇到很nice的同胞~也不知道放水了没有,总之感谢内推的亲和面我的面试官。欢迎指正 ~~ ^ ^
1. BST, given a min and a max, can you trim the tree so that the remaining nodes are within the range of (min, max)?
Node* trim(Node *root,int min,int max)
{
Node *left,*right;
if(NULL==root)
. 1point3acres.com return NULL;
if(root->value>min&&root->value<max)
{
left = trim(root->left,min,max);
right = trim(root->right,min,max);
root->left = left,root->right = right;
return root;
}else if(root->value>=max)
{
left = trim(root->left,min,max);.
return left;.1point3acres
}else
{
right = trim(root->right,min,max);
return right;
}.
}. ----
2. Given a matrix, it’s sorted by column and by row, can you design an algorithm to find the target. 1point3acres
boolean check(vector< vector<int> > &matrix, int &target) {
int row,col;
row = matrix.size();
if(row==0) return false;
col = matrix[0].size();
if(col==0) return false;
int rowIndex,colIndex;
rowIndex = 0,colIndex = col-1;
while(rowIndex<=row-1&&colIndex>=0)
{
//find target
if(maxtrix[rowIndex][colIndex]==target)
return true;
else if(matrix[rowIndex][colIndex]<target)
{//target is smaller
colIndex--;
}else.--
{//target is bigger
rowIndex++;
}. 1point3acres
}
return false;. .и
}
|
|