avatar
Google second phone interview# JobHunting - 待字闺中
i*6
1
1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
1
/ \
2 -> 3
/ \ / \
4->5->6->7
2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
经由二维的整数数组给出。
都要求说思路,然后doc上面coding.
avatar
h*l
2
1.bfs,存level和pre ptr把
2.cc150上面的题

【在 i*******6 的大作中提到】
: 1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
: 1
: / \
: 2 -> 3
: / \ / \
: 4->5->6->7
: 2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
: 经由二维的整数数组给出。
: 都要求说思路,然后doc上面coding.

avatar
r*n
3
1用deque就是了

【在 h**********l 的大作中提到】
: 1.bfs,存level和pre ptr把
: 2.cc150上面的题

avatar
h*l
4
还是要记下每层头尾把

【在 r*******n 的大作中提到】
: 1用deque就是了
avatar
r*n
5
不用...头上蹦出来, 尾巴插child

【在 h**********l 的大作中提到】
: 还是要记下每层头尾把
avatar
g*e
6
my sol. for 1
use two queues,
push root to q1,
pop q1, connect all nodes in q1. push all its children to q2.
pop q2, connect all nodes in q2. push all their children to q1.
then two queues do this in turn.
avatar
d*o
7
第一题用先广遍历,然后把每一个node都放到一个vector里面去。
接着把vector里面的node每一个连到下一个(判断一下是否是最右节点)
有人可以不用多余空间的方法吗?

【在 i*******6 的大作中提到】
: 1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
: 1
: / \
: 2 -> 3
: / \ / \
: 4->5->6->7
: 2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
: 经由二维的整数数组给出。
: 都要求说思路,然后doc上面coding.

avatar
l*a
8
有啊
不需要o(N)空间
以前有人发过

【在 d****o 的大作中提到】
: 第一题用先广遍历,然后把每一个node都放到一个vector里面去。
: 接着把vector里面的node每一个连到下一个(判断一下是否是最右节点)
: 有人可以不用多余空间的方法吗?

avatar
r*n
9
treewidth也是worst case O(n)啊...

【在 l*****a 的大作中提到】
: 有啊
: 不需要o(N)空间
: 以前有人发过

avatar
d*o
10
顶这个,不过你这个方法还是需要traversal所有的节点。
写一下,看看有没有问题。
void sib(node* root)
{
if(root&&root->left&&root->right)
{
root->left->next = root->right;
if(root->next)
{
root->right->next = root->next->left;
}
sib(root->left);
sib(root->right);
}
}
avatar
q*c
11
Bingo!
avatar
l*a
12
没有任何一个方法不需要traverse所有的节电吧?

【在 d****o 的大作中提到】
: 顶这个,不过你这个方法还是需要traversal所有的节点。
: 写一下,看看有没有问题。
: void sib(node* root)
: {
: if(root&&root->left&&root->right)
: {
: root->left->next = root->right;
: if(root->next)
: {
: root->right->next = root->next->left;

avatar
a*g
13
I used exactly the same solution in MS interview. and the interviewer asked
me to come up a solution with O(1) space

【在 d****o 的大作中提到】
: 第一题用先广遍历,然后把每一个node都放到一个vector里面去。
: 接着把vector里面的node每一个连到下一个(判断一下是否是最右节点)
: 有人可以不用多余空间的方法吗?

avatar
c*n
14
For the first question:
// Assumption: all node's sibling property is null before calling
// the function.
void linkSibling(Node root){
if (root == null) return;
if (root.left != null){
root.left.sibling = root.right;
}
if (root.right != null ){
root.right.sibling = root.sibling == null ? null : root.sibling.left;
}
linkSibling(root.left);
linkSibling(root.right);
}
avatar
a*s
16
mark
avatar
d*o
18
他是不是就想要我上面写的recursion版本?不过recursion也需要stack,需要O(n)
space啊?
你过了没有?

asked

【在 a*****g 的大作中提到】
: I used exactly the same solution in MS interview. and the interviewer asked
: me to come up a solution with O(1) space

avatar
i*6
19
1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
1
/ \
2 -> 3
/ \ / \
4->5->6->7
2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
经由二维的整数数组给出。
都要求说思路,然后doc上面coding.
avatar
h*l
20
1.bfs,存level和pre ptr把
2.cc150上面的题

【在 i*******6 的大作中提到】
: 1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
: 1
: / \
: 2 -> 3
: / \ / \
: 4->5->6->7
: 2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
: 经由二维的整数数组给出。
: 都要求说思路,然后doc上面coding.

avatar
r*n
21
1用deque就是了

【在 h**********l 的大作中提到】
: 1.bfs,存level和pre ptr把
: 2.cc150上面的题

avatar
h*l
22
还是要记下每层头尾把

【在 r*******n 的大作中提到】
: 1用deque就是了
avatar
r*n
23
不用...头上蹦出来, 尾巴插child

【在 h**********l 的大作中提到】
: 还是要记下每层头尾把
avatar
g*e
24
my sol. for 1
use two queues,
push root to q1,
pop q1, connect all nodes in q1. push all its children to q2.
pop q2, connect all nodes in q2. push all their children to q1.
then two queues do this in turn.
avatar
d*o
25
第一题用先广遍历,然后把每一个node都放到一个vector里面去。
接着把vector里面的node每一个连到下一个(判断一下是否是最右节点)
有人可以不用多余空间的方法吗?

【在 i*******6 的大作中提到】
: 1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
: 1
: / \
: 2 -> 3
: / \ / \
: 4->5->6->7
: 2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
: 经由二维的整数数组给出。
: 都要求说思路,然后doc上面coding.

avatar
l*a
26
有啊
不需要o(N)空间
以前有人发过

【在 d****o 的大作中提到】
: 第一题用先广遍历,然后把每一个node都放到一个vector里面去。
: 接着把vector里面的node每一个连到下一个(判断一下是否是最右节点)
: 有人可以不用多余空间的方法吗?

avatar
r*n
27
treewidth也是worst case O(n)啊...

【在 l*****a 的大作中提到】
: 有啊
: 不需要o(N)空间
: 以前有人发过

avatar
d*o
28
顶这个,不过你这个方法还是需要traversal所有的节点。
写一下,看看有没有问题。
void sib(node* root)
{
if(root&&root->left&&root->right)
{
root->left->next = root->right;
if(root->next)
{
root->right->next = root->next->left;
}
sib(root->left);
sib(root->right);
}
}

【在 q********c 的大作中提到】
: Bingo!
avatar
q*c
29
Bingo!
avatar
l*a
30
没有任何一个方法不需要traverse所有的节电吧?

【在 d****o 的大作中提到】
: 顶这个,不过你这个方法还是需要traversal所有的节点。
: 写一下,看看有没有问题。
: void sib(node* root)
: {
: if(root&&root->left&&root->right)
: {
: root->left->next = root->right;
: if(root->next)
: {
: root->right->next = root->next->left;

avatar
a*g
31
I used exactly the same solution in MS interview. and the interviewer asked
me to come up a solution with O(1) space

【在 d****o 的大作中提到】
: 第一题用先广遍历,然后把每一个node都放到一个vector里面去。
: 接着把vector里面的node每一个连到下一个(判断一下是否是最右节点)
: 有人可以不用多余空间的方法吗?

avatar
c*n
32
For the first question:
// Assumption: all node's sibling property is null before calling
// the function.
void linkSibling(Node root){
if (root == null) return;
if (root.left != null){
root.left.sibling = root.right;
}
if (root.right != null ){
root.right.sibling = root.sibling == null ? null : root.sibling.left;
}
linkSibling(root.left);
linkSibling(root.right);
}
avatar
a*s
34
mark
avatar
d*o
36
他是不是就想要我上面写的recursion版本?不过recursion也需要stack,需要O(n)
space啊?
你过了没有?

asked

【在 a*****g 的大作中提到】
: I used exactly the same solution in MS interview. and the interviewer asked
: me to come up a solution with O(1) space

avatar
j*2
37
should be O(log n) space ba. The recursion depth is log n for a perfect tree
ba?

【在 d****o 的大作中提到】
: 他是不是就想要我上面写的recursion版本?不过recursion也需要stack,需要O(n)
: space啊?
: 你过了没有?
:
: asked

avatar
n*i
38
第一题 O(1)空间可以实现。思路是:
要想link 第 n+1 行 node 的 next right, 需要从 第 n 行 node 的 第一个下手。
假设 Node *n 是第n行第一个node
while(n && n->nextRight){
n->left->nextRight = n->right;
n->right->nextRight = n->nextRight->left;
n = n->nextRight;
}
n->left->nextRight = n->right;
n->right->nextRight = 0;
这样就可以把第n+1 行 连起来。 这里利用的是当你连第n+1行时,第n行已经连好。

【在 i*******6 的大作中提到】
: 1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
: 1
: / \
: 2 -> 3
: / \ / \
: 4->5->6->7
: 2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
: 经由二维的整数数组给出。
: 都要求说思路,然后doc上面coding.

avatar
n*i
39
第一题 O(1)空间可以实现。思路是:
要想link 第 n+1 行 node 的 next right, 需要从 第 n 行 node 的 第一个下手。
假设 Node *n 是第n行第一个node
while(n && n->nextRight){
n->left->nextRight = n->right;
n->right->nextRight = n->nextRight->left;
n = n->nextRight;
}
n->left->nextRight = n->right;
n->right->nextRight = 0;
这样就可以把第n+1 行 连起来。 这里利用的是当你连第n+1行时,第n行已经连好。

【在 i*******6 的大作中提到】
: 1.perfect binary tree, 给每一个node加一个sabling的pointer,比如
: 1
: / \
: 2 -> 3
: / \ / \
: 4->5->6->7
: 2.实现画图里面的油漆桶功能(那个点下去可以把一块图染成一种颜色的),假设图已
: 经由二维的整数数组给出。
: 都要求说思路,然后doc上面coding.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。