package main
import "fmt"
// TreeNode represents a node in a tree
type TreeNode struct {
Val int
Children []*TreeNode
}
// PrintTree prints tree nodes with indentation to show hierarchy
func PrintTree(node *TreeNode, level int) {
if node == nil {
return
}
for i := 0; i < level; i++ {
fmt.Print(" ")
}
fmt.Println(node.Val)
for _, child := range node.Children {
PrintTree(child, level+1) // recurse to print children with indent
}
}
// PrintArray prints array elements in one line
func PrintArray(arr []int) {
fmt.Print("[")
for i, v := range arr {
fmt.Print(v)
if i != len(arr)-1 {
fmt.Print(", ")
}
}
fmt.Println("]")
}
// ListNode represents a node in a linked list
type ListNode struct {
Val int
Next *ListNode
}
// PrintLinkedList prints linked list nodes in order
func PrintLinkedList(head *ListNode) {
curr := head
for curr != nil {
fmt.Printf("%d", curr.Val)
if curr.Next != nil {
fmt.Print(" -> ")
}
curr = curr.Next // advance to next node
}
fmt.Println(" -> null")
}
func main() {
// Build tree
root := &TreeNode{Val: 1}
child2 := &TreeNode{Val: 2}
child3 := &TreeNode{Val: 3}
child4 := &TreeNode{Val: 4}
child5 := &TreeNode{Val: 5}
child6 := &TreeNode{Val: 6}
child2.Children = []*TreeNode{child4}
child3.Children = []*TreeNode{child5, child6}
root.Children = []*TreeNode{child2, child3}
// Array
arr := []int{1, 2, 3, 4, 5, 6}
// Linked List
head := &ListNode{Val: 1}
n2 := &ListNode{Val: 2}
n3 := &ListNode{Val: 3}
n4 := &ListNode{Val: 4}
n5 := &ListNode{Val: 5}
n6 := &ListNode{Val: 6}
head.Next = n2
n2.Next = n3
n3.Next = n4
n4.Next = n5
n5.Next = n6
fmt.Println("Tree structure:")
PrintTree(root, 0)
fmt.Println("Array elements:")
PrintArray(arr)
fmt.Println("Linked List nodes:")
PrintLinkedList(head)
}
for _, child := range node.Children {
PrintTree(child, level+1)
recurse to print each child with increased indentation to show hierarchy
curr = curr.Next // advance to next node
move forward in linked list to print nodes in order
Tree structure:
1
2
4
3
5
6
Array elements:
[1, 2, 3, 4, 5, 6]
Linked List nodes:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null