Redian新闻
>
求教一道经典面题的解法
avatar
求教一道经典面题的解法# JobHunting - 待字闺中
D*6
1
print binary tree in level order, starting from the top, an alternate is
starting from the lowest level. Starting a new line for each level.
这题是不是就用个queue 加个 counter, count读到的node数量, print the new line
when counter gets to 1, 2, 4, 8, .....还有就是children是null也要加到
queue里去make sure counter behaves correct.
大家都是这么做的吗? 有啥其他的方法吗?
谢谢
avatar
z*n
2
complete binary tree才能用1, 2, 4, 8
从顶上开始打印的话,再将root放入queue后,放一个newline的标志进入queue(比如一
个特殊node),以后循环处理queue的时候,遇到这个newline标志就打印个newline,然
后把这个newline标志重新放入queue(这时它出现在queue最后,表示下一次要打印new
line的时候)

line

【在 D****6 的大作中提到】
: print binary tree in level order, starting from the top, an alternate is
: starting from the lowest level. Starting a new line for each level.
: 这题是不是就用个queue 加个 counter, count读到的node数量, print the new line
: when counter gets to 1, 2, 4, 8, .....还有就是children是null也要加到
: queue里去make sure counter behaves correct.
: 大家都是这么做的吗? 有啥其他的方法吗?
: 谢谢

avatar
r*o
3
如果要从底部开始打印呢?是不是要把这些结果放到stack里面先?

如一
,然
new

【在 z****n 的大作中提到】
: complete binary tree才能用1, 2, 4, 8
: 从顶上开始打印的话,再将root放入queue后,放一个newline的标志进入queue(比如一
: 个特殊node),以后循环处理queue的时候,遇到这个newline标志就打印个newline,然
: 后把这个newline标志重新放入queue(这时它出现在queue最后,表示下一次要打印new
: line的时候)
:
: line

avatar
z*n
4
恩,就是处理queue的时候,不直接打印,放到一个stack里面,然后从stack里打印,不
过这样的话,每一行的顺序都反了。要是还要求每一行的顺序跟原来一样。。。貌似没
有什么简洁漂亮的方法。。。

【在 r****o 的大作中提到】
: 如果要从底部开始打印呢?是不是要把这些结果放到stack里面先?
:
: 如一
: ,然
: new

avatar
z*n
5
要是从底层开始打印,并且还要维持每层顺序的话,貌似只能处理queue的时候,把每一
行加入另外一个queue,遇到newline,就把这另外一个queue整体作为一个对象放到另一
个stack里。。。

,不

【在 z****n 的大作中提到】
: 恩,就是处理queue的时候,不直接打印,放到一个stack里面,然后从stack里打印,不
: 过这样的话,每一行的顺序都反了。要是还要求每一行的顺序跟原来一样。。。貌似没
: 有什么简洁漂亮的方法。。。

avatar
r*o
6
处理每行的时候反序将children节点放进qeueue里不行吗?

,不

【在 z****n 的大作中提到】
: 恩,就是处理queue的时候,不直接打印,放到一个stack里面,然后从stack里打印,不
: 过这样的话,每一行的顺序都反了。要是还要求每一行的顺序跟原来一样。。。貌似没
: 有什么简洁漂亮的方法。。。

avatar
z*n
7
恩,可以,土了。。。

【在 r****o 的大作中提到】
: 处理每行的时候反序将children节点放进qeueue里不行吗?
:
: ,不

avatar
c*y
8
Can you implement the binary tree using an array, and then print out the
array?
Since the level numbers of the elements in the tree can be determined using
their positions in the array and they are placed in the array in the order
of level-increasing, it is easy to print out the elements in the same level.

line

【在 D****6 的大作中提到】
: print binary tree in level order, starting from the top, an alternate is
: starting from the lowest level. Starting a new line for each level.
: 这题是不是就用个queue 加个 counter, count读到的node数量, print the new line
: when counter gets to 1, 2, 4, 8, .....还有就是children是null也要加到
: queue里去make sure counter behaves correct.
: 大家都是这么做的吗? 有啥其他的方法吗?
: 谢谢

avatar
D*6
9
这个不太对吧, 每次add newline时是针对这个current node, 不是这个current level
, 所以还是需要1,2,4,8来分辨什么时候打newline. 不complete 也可以, 就需要把
null children也要add到queue里. 然后每次看到null就只是count++, continue.
我刚开始也push newline into the queue, then realize doesn't work!

如一
,然
new

【在 z****n 的大作中提到】
: complete binary tree才能用1, 2, 4, 8
: 从顶上开始打印的话,再将root放入queue后,放一个newline的标志进入queue(比如一
: 个特殊node),以后循环处理queue的时候,遇到这个newline标志就打印个newline,然
: 后把这个newline标志重新放入queue(这时它出现在queue最后,表示下一次要打印new
: line的时候)
:
: line

avatar
z*n
10
如果看到null就是简单的count++,那么你的方法也可以
不过我的方法也是对的,你再琢磨琢磨:)

level

【在 D****6 的大作中提到】
: 这个不太对吧, 每次add newline时是针对这个current node, 不是这个current level
: , 所以还是需要1,2,4,8来分辨什么时候打newline. 不complete 也可以, 就需要把
: null children也要add到queue里. 然后每次看到null就只是count++, continue.
: 我刚开始也push newline into the queue, then realize doesn't work!
:
: 如一
: ,然
: new

avatar
D*6
11
明白了! 多谢!!

【在 z****n 的大作中提到】
: 如果看到null就是简单的count++,那么你的方法也可以
: 不过我的方法也是对的,你再琢磨琢磨:)
:
: level

avatar
s*n
12
1.from top
Use two queues q1 and q2. Get a node from the first queue, print it, and
then push its children into the second queue.When the first queue is empty,
get nodes from the second queue and push their children into the first queue
, and so on...
2. from bottom
Use two queue q1 and q2, and one stack s1. The stack is used to store nodes.
When all nodes are visited, get nodes from the stack and print. Similarly
to the above, except that when you get a node from one queue, first push its
right
avatar
s*n
13
2. from bottom
You need to insert a dummy node into the stack as line end mark.
avatar
t*h
14
我一直用dummy node来做 但是昨天看到有人说有更简单的方法 请知道的赐教
我的做法 是一个队列 一个栈
循环开始前把root和dummy入队列
从队列里读结点
把这个点放到栈中 然后把他的子结点入队列
当读到dummy时 如果队列不为空 再把dummy入队列 压栈
如果队列为空 则表示从栈中输出结果 读到dummy时输出newline
avatar
h*k
15
one queue is enough.

,
queue
nodes.
its

【在 s*****n 的大作中提到】
: 1.from top
: Use two queues q1 and q2. Get a node from the first queue, print it, and
: then push its children into the second queue.When the first queue is empty,
: get nodes from the second queue and push their children into the first queue
: , and so on...
: 2. from bottom
: Use two queue q1 and q2, and one stack s1. The stack is used to store nodes.
: When all nodes are visited, get nodes from the stack and print. Similarly
: to the above, except that when you get a node from one queue, first push its
: right

avatar
s*n
16
Of course, one queue can do it. Using two queues is just to remove dummy
node in the queue.
avatar
a*d
17
One queue does not save you anything.
The total number of elements are the same.

【在 h**k 的大作中提到】
: one queue is enough.
:
: ,
: queue
: nodes.
: its

avatar
k*i
18
从root 分层打印 可以这样写 只需要一个queue和一个mark
void level_traversal(Node * root)
{
if (!root)
exit(0);
Queue my_queue = new Queue();
my_queue.push(root);
my_queue.push(Mark);


while (my_queue.empty())
{
Node tmp = my_queue.pop();

if (tmp==Mark)
{
my_queue.push(Mark);
Print("\n");
}
else
{
Print(tmp);
my_queue.push(root->left);
my

【在 D****6 的大作中提到】
: print binary tree in level order, starting from the top, an alternate is
: starting from the lowest level. Starting a new line for each level.
: 这题是不是就用个queue 加个 counter, count读到的node数量, print the new line
: when counter gets to 1, 2, 4, 8, .....还有就是children是null也要加到
: queue里去make sure counter behaves correct.
: 大家都是这么做的吗? 有啥其他的方法吗?
: 谢谢

avatar
h*h
19
从root开始分层打印
在你代码修改了一下,不用mark. 我对C++不太了,明白我的idea就好。我在一个最后
哪到offer的公司onsite面到。临时想的,不保证最简单。当时用的是java.
void level_traversal(Node * root)
{
if (!root)
exit(0);
Queue my_queue = new Queue();
my_queue.push(root);
while (my_queue.empty())
{
int count = my_queue.size();
for(int i = 0; i < count; ++i){
Node tmp = my_queue.pop();
Print(tmp);
if(temp->left) my_queue.push(temp->left);
if(temp->right) my_queue.push(t

【在 k**********i 的大作中提到】
: 从root 分层打印 可以这样写 只需要一个queue和一个mark
: void level_traversal(Node * root)
: {
: if (!root)
: exit(0);
: Queue my_queue = new Queue();
: my_queue.push(root);
: my_queue.push(Mark);
:
:

avatar
a*g
20
你这个不是通用queue了
常规的queue是不能直接调用size()的
当然,c++的deque是可以的

【在 h****h 的大作中提到】
: 从root开始分层打印
: 在你代码修改了一下,不用mark. 我对C++不太了,明白我的idea就好。我在一个最后
: 哪到offer的公司onsite面到。临时想的,不保证最简单。当时用的是java.
: void level_traversal(Node * root)
: {
: if (!root)
: exit(0);
: Queue my_queue = new Queue();
: my_queue.push(root);
: while (my_queue.empty())

avatar
h*h
21
那个语言的queue不能调用size()?
C++的queue也可以调用size()啊。这个问题又用不到deque。
实在不准用size(), 自己count也行。
个人只是觉得用dummy node 太weird.

【在 a***g 的大作中提到】
: 你这个不是通用queue了
: 常规的queue是不能直接调用size()的
: 当然,c++的deque是可以的

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