Solution Review: Print Spiral Tree
Let’s go through the solution review of the spiral tree printing problem in this lesson.
We'll cover the following...
Solution
Stacks follow the LIFO principle, so two stacks are used to process each level alternatively. The nodes are added and processed in such an order that nodes are printed in spiral order. Let’s look at how we can do this in code.
Solution code
Press + to interact
main.go
tree.go
stack.go
package mainimport "fmt"func (t *Tree) PrintSpiralTree() {stk1 := new(Stack)stk2 := new(Stack)var temp *Nodeif t.root != nil {stk1.Push(t.root)}for stk1.Length() != 0 || stk2.Length() != 0 {for stk1.Length() != 0 {temp = stk1.Pop()fmt.Print(temp.value, " ")if temp.left != nil {stk2.Push(temp.left)}if temp.right != nil {stk2.Push(temp.right)}}fmt.Println(" ")for stk2.Length() != 0{temp = stk2.Pop()fmt.Print(temp.value, " ")if temp.right != nil {stk1.Push(temp.right)}if temp.left != nil {stk1.Push(temp.left)}}fmt.Println(" ")}}/* Testing Code */func main() {arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}t := LevelOrderBinaryTree(arr)t.PrintSpiralTree()}
Complexity analysis
The ...