posts - 0,  comments - 0,  trackbacks - 0
非递归后续遍历修改为:
public static void iterativePostorder(BinaryTree boot) {
Stack<BinaryTree> stack = new Stack<BinaryTree>();
BinaryTree current, pointer=boot;
boolean bVisitRoot = false;//标志是否访问过根节点
if (boot == null) {
return;
}
stack.push(boot);
current = boot.leftpoiter;
while (!stack.empty()) {
while (current != null) {//向左走到尽头
stack.push(current);
current = current.leftpoiter;
}
current = stack.peek();
stack.pop();
if ((current.rightpoiter == null)||(pointer == current.rightpoiter)) {
visit(current);
pointer = current;
current = stack.peek();
if (pointer == current.rightpoiter) {
visit(current);
stack.pop();
pointer=current;
current = null;
} else {
current = current.rightpoiter;
}
} else {
if (current == boot && (bVisitRoot == true)) {
visit(current);
return;
} else {
if (current == boot) {
bVisitRoot = true;
}
stack.push(current);
current = current.rightpoiter;
}
}

}

}
<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

搜索

  •  

最新评论