📖 Scenario: Imagine you are building a simple family tree app. Each person can have up to two children. You want to create the basic structure to hold each person and their children.
🎯 Goal: Create a binary tree node structure in Go to represent a person and their two children.
📋 What You'll Learn
Define a struct called TreeNode with an integer field Value
Add two pointer fields Left and Right of type *TreeNode
Create a root node with value 10
Assign left child with value 5 and right child with value 15
Print the root node's value and its children's values
💡 Why This Matters
🌍 Real World
Binary trees are used in many applications like organizing data, searching quickly, and representing hierarchical relationships such as family trees or file systems.
💼 Career
Understanding how to create and manipulate tree structures is important for software development roles involving data organization, algorithms, and system design.
Progress0 / 4 steps
1
Define the TreeNode struct
Define a struct called TreeNode with an integer field Value and two pointer fields Left and Right of type *TreeNode.
DSA Go
Hint
Use type TreeNode struct {} to define the structure. Inside, add Value int, Left *TreeNode, and Right *TreeNode.
2
Create the root node and children
Inside main, create a variable root of type *TreeNode with Value 10. Then assign its Left child with a TreeNode of Value 5 and its Right child with a TreeNode of Value 15.
DSA Go
Hint
Use &TreeNode{Value: 10} to create a pointer to a new node. Assign children similarly.
3
Add a helper variable for printing
Create a variable called nodes as a slice of pointers to TreeNode containing root, root.Left, and root.Right.
DSA Go
Hint
Use a slice literal like nodes := []*TreeNode{root, root.Left, root.Right}.
4
Print the values of root and its children
Use a for loop with variable node to iterate over nodes and print each node's Value using fmt.Println(node.Value). Import fmt package at the top.
DSA Go
Hint
Use for _, node := range nodes and inside the loop fmt.Println(node.Value).
Practice
(1/5)
1. What is the correct way to define a node structure for a binary tree in Go?
easy
A. type Node struct { Value int; Left Node; Right Node }
B. type Node struct { Value int; Left *Node; Right *Node }
C. type Node struct { Value int; Left int; Right int }
D. type Node struct { Value *int; Left *int; Right *int }
Solution
Step 1: Understand binary tree node components
A binary tree node stores a value and pointers to left and right child nodes.
Step 2: Check pointer usage in Go struct
Using pointers (*Node) for Left and Right allows linking nodes without copying entire structs.
Final Answer:
type Node struct { Value int; Left *Node; Right *Node } -> Option B
Quick Check:
Node with pointers = type Node struct { Value int; Left *Node; Right *Node } [OK]
Hint: Use pointers for child nodes to avoid copying structs [OK]
Common Mistakes:
Using non-pointer types for child nodes causes infinite recursion
Using int instead of *Node for children
Using pointers to int instead of pointers to Node
2. Which of the following is the correct syntax to create a new binary tree node with value 10 in Go?
easy
A. node := Node{Value: 10, Left: nil, Right: nil}
B. node := Node(10, nil, nil)
C. node := &Node{Value: 10, Left: nil, Right: nil}
D. node := new(Node{10, nil, nil})
Solution
Step 1: Understand node creation with pointers
To create a pointer to a Node struct, use &Node{...} syntax.
Step 2: Check each option's syntax
node := &Node{Value: 10, Left: nil, Right: nil} correctly creates a pointer to a Node with value 10 and nil children.