g********d 发帖数: 203 | 1 How can you traverse a binary tree without recursion or stack? |
t****a 发帖数: 1212 | 2 if queue is allowed you can do BFS |
B*****t 发帖数: 335 | 3 threading a tree, although slower, but save memory.
【在 g********d 的大作中提到】 : How can you traverse a binary tree without recursion or stack?
|
y*********e 发帖数: 518 | 4 分层遍历,也可以用 vector 。
void levelTraverse(Tree root) {
Vector thisLevel = new Vector();
Vector nextLevel = new Vector();
thisLevel.add(root);
while (!thisLevel.isEmpty()) {
foreach (Tree tree in thisLevel) {
print(tree);
nextLevel.addAll(tree.children());
}
thisLevel.clear();
thisLevel.addAll(nextLevel);
nextLevel.clear();
println();
}
} |
H*****L 发帖数: 5705 | 5 那么vector的属性怎么实现的?
【在 y*********e 的大作中提到】 : 分层遍历,也可以用 vector 。 : void levelTraverse(Tree root) { : Vector thisLevel = new Vector(); : Vector nextLevel = new Vector(); : thisLevel.add(root); : while (!thisLevel.isEmpty()) { : foreach (Tree tree in thisLevel) { : print(tree); : nextLevel.addAll(tree.children()); : }
|
c*******w 发帖数: 63 | 6 Morris Traversal may solve this problem |