r****o 发帖数: 1950 | 1 一种方法是用queue和stack,还有别的方法吗? |
s******t 发帖数: 2374 | |
r****o 发帖数: 1950 | 3 是啊,怎么打印比较好?
【在 s******t 的大作中提到】 : 你是说倒着的BFS?
|
s******t 发帖数: 2374 | 4 正常bfs打印到一个临时的buffer中,然后从buf打出倒序?
p |
s********a 发帖数: 1447 | 5 用queue
要visit的从dequeue
fringe node enqueue
直到所有的都遍历完
【在 r****o 的大作中提到】 : 是啊,怎么打印比较好?
|
r****o 发帖数: 1950 | 6 要visit的从dequeue是什么意思
【在 s********a 的大作中提到】 : 用queue : 要visit的从dequeue : fringe node enqueue : 直到所有的都遍历完
|
l*******g 发帖数: 8 | 7 一个queue应该就可以了,我把code贴出来了。如果有错误的话,请指正。
printByLevel(Node *root)
{
Queue queue;
int currLevel = 1; /* total number of nodes on the current level */
int currPos = 0; /* current position on the current level */
int nextLevel = 0; /* total number of nodes on the next level */
queue.enqueue(root);
while(currLevel>0)
{
Node *curr = queue.dequeue();
printNode(curr);
if(curr->left){
queue.enqueue(curr->left);
nextL |
s******t 发帖数: 2374 | 8 他的意思是要反向打印
比如:
a
b c
d e f g
打印成
d e f g
b c
a
*/
*/
【在 l*******g 的大作中提到】 : 一个queue应该就可以了,我把code贴出来了。如果有错误的话,请指正。 : printByLevel(Node *root) : { : Queue queue; : int currLevel = 1; /* total number of nodes on the current level */ : int currPos = 0; /* current position on the current level */ : int nextLevel = 0; /* total number of nodes on the next level */ : queue.enqueue(root); : while(currLevel>0) : {
|
c******f 发帖数: 2144 | |
r****o 发帖数: 1950 | 10 自己顶一下。
【在 r****o 的大作中提到】 : 一种方法是用queue和stack,还有别的方法吗?
|
r****o 发帖数: 1950 | 11 请各位大牛出招啊。
【在 r****o 的大作中提到】 : 一种方法是用queue和stack,还有别的方法吗?
|
s******t 发帖数: 2374 | 12 queue或者stack不是挺好的么?为啥一定要其他的?转化成递归?我不sure是不是能做
。。。没仔细想过。
BTW:我不是大牛。。。。我是打酱油的。
【在 r****o 的大作中提到】 : 请各位大牛出招啊。
|
r****o 发帖数: 1950 | 13 以前看到过版上的牛人讨论过一个很好的方法,找不到链接了。
【在 s******t 的大作中提到】 : queue或者stack不是挺好的么?为啥一定要其他的?转化成递归?我不sure是不是能做 : 。。。没仔细想过。 : BTW:我不是大牛。。。。我是打酱油的。
|