struct Node { Value value; Node * left; Node * right; } void printBT(Node * root); void printBT(vector & v1); void printBT(Node * root) { vector v1; v1.push_back(root); printBT(v1); } // recursively printBT Date structure: using two vectors -- v1 holds nodes of current level, v2 hold nodes of next level; void printBT(vector & v1) { if ( v1.empty() ) return; vector v2; Node * node; for (int i = 0; i < v1.size(); i++) // Push all children of v1 into v2; { node = v1[i]; if (node->left != NULL) v2.push_back( node->left ) if (node->right != NULL) v2.push_back( node->right ) } printBT(v2); // call function PrintBT (v2), if v2 is empty, then means no nodes at this level, just return; for (int i = 0; i < v1.size(); i++) // print nodes at current level { node = v[i]; cout << node->value << " "; } cout << endl; }
【在 a*******y 的大作中提到】 : 能用另外一个stack来保存结果,最后再打印,但是假如让你每个level之间print 换行 : ,怎么办? : 这题的变化是,给你任何一个height的level,start from there to print up
x*i
8 楼
i thibk bce bcp both cap 6k both calendar year.
m*q
9 楼
谢谢了,版主
a*y
10 楼
唉,你们这都是什么啊 那个明白人来给说说
s*r
11 楼
是bcp,多谢楼上两位指正和回答。 另外有个问题: 那这6000是按照刷卡charge的日期算还是按照给cash back credit的日期算,貌似需要 下下个statement才能出6%的credit。 比如说我今年十二月份刷了6000的grocery,照理应该明年一月或二月的statemnt才会 出来 statement credit。那我明年再刷6000的grocery,还会拿到6%的statement credit吗? citi dividend是按照post cashback的日期算的,不知道amex是不是也这么干。
So, I think that another BULL GUY is using recursive to push_back the lower layer's values into container. And when it reaches the lowest level begin to print, then the 2nd lowest layer's values printed. I am trying to use non-recursive means. From the highest level, push the node into a queue (for next level) and a stack for final printing. When the level popped out from the queue increases to the lower level, check whether it is the objective. If yes, stop and print everything in the stack. Because it is a LIFO, the printed result is like the lower layer printed first. If no, continue to pop the node from the queue and push this node's leaves into this queue and the stack... until you reach the level you need.
m*q
18 楼
有谁知道这个6000的cutoff怎么算么?
Z*Z
19 楼
我觉得你的思路是对的。实现上,同递归的版本比起来,有些可以改进。。。 假设树是full and complete的,一共有N个节点。那个递归的写法的空间复杂度 的大头 就是N。而你的写法,到最后,栈里面存了所有的N个节点,队列里面还有最后一层N/2 个节点,一共是3N/2,再加上你貌似给每个node还加了一个level的field。。。
lower the whether Because If into
【在 l****c 的大作中提到】 : So, I think that another BULL GUY is using recursive to push_back the lower : layer's values into container. And when it reaches the lowest level begin : to print, then the 2nd lowest layer's values printed. : I am trying to use non-recursive means. From the highest level, push the : node into a queue (for next level) and a stack for final printing. When the : level popped out from the queue increases to the lower level, check whether : it is the objective. If yes, stop and print everything in the stack. Because : it is a LIFO, the printed result is like the lower layer printed first. If : no, continue to pop the node from the queue and push this node's leaves into : this queue and the stack... until you reach the level you need.
l*c
20 楼
Yes, as a beginner, I still could not consider the things like complexity. The level I could reach is to finish question...... How to improve? In addition, the reason I added the level is that lz's asking to print the values of levels higher than a particular one.
大头 /2
【在 Z*****Z 的大作中提到】 : 我觉得你的思路是对的。实现上,同递归的版本比起来,有些可以改进。。。 : 假设树是full and complete的,一共有N个节点。那个递归的写法的空间复杂度 的大头 : 就是N。而你的写法,到最后,栈里面存了所有的N个节点,队列里面还有最后一层N/2 : 个节点,一共是3N/2,再加上你貌似给每个node还加了一个level的field。。。 : : lower : the : whether : Because : If