本文共 1461 字,大约阅读时间需要 4 分钟。
preorder(node) if node == null then return visit(node) preorder(node.left) preorder(node.right) | iterativePreorder(node) parentStack = empty stack parentStack.push(null) top = node while ( top != null ) visit( top ) if ( top.right != null ) parentStack.push(top.right) if ( top.left != null ) parentStack.push(top.left) top = parentStack.top(); parentStack.pop(); |
In-order[]
inorder(node) if node == null then return inorder(node.left) visit(node) inorder(node.right) | iterativeInorder(node) parentStack = empty stack while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) parentStack.push(node) node = node.left else node = parentStack.pop() visit(node) node = node.right |
Post-order[]
postorder(node) if node == null then return postorder(node.left) postorder(node.right) visit(node) | iterativePostorder(node) parentStack = empty stack lastnodevisited = null while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) parentStack.push(node) node = node.left else peeknode = parentStack.peek() if (peeknode.right ≠ null and lastnodevisited ≠ peeknode.right) /* if right child exists AND traversing node from left child, move right */ node = peeknode.right else parentStack.pop() visit(peeknode) lastnodevisited = peeknode |
转载于:https://www.cnblogs.com/cavehubiao/p/3611186.html