code kata 230808

This commit is contained in:
VicRen
2023-08-08 22:08:51 +08:00
parent 5f7b76c60a
commit 02e1a8ad1a
7 changed files with 471 additions and 2 deletions
+30
View File
@@ -111,6 +111,31 @@ func postOrder2(n *node) {
}
}
func postOrder22(n *node) {
stack := NewStack()
p := n
var r *node = nil
for p != nil || !stack.IsEmpty() {
if p != nil {
stack.Push(p)
p = p.left
} else {
p = stack.Top().(*node)
if p.right != nil && p.right != r {
p = p.right
stack.Push(p)
p = p.left
} else {
x, _ := stack.Pop()
p = x.(*node)
visit(p)
r = p
p = nil
}
}
}
}
func visit(n *node) {
fmt.Printf("Visiting Node: %s\n", n.value)
}
@@ -167,3 +192,8 @@ func TestPostOrder2(t *testing.T) {
n := makeBinaryTree()
postOrder2(n)
}
func TestPostOrder22(t *testing.T) {
n := makeBinaryTree()
postOrder22(n)
}