# Golang Program Left View of Binary Tree

In programming, there is a coding problem of binary tree that gets asked very frequently in interviews and the problem statement is to find the left view of a binary tree. If we try to understand the problem statement more than what exactly the left view is then we can explain it in a way that all the nodes are visible when you see them by standing on the left side of the tree.

## Illustration

Let us understand more with the help of an example. Suppose we have a below tree and if we stand on the left side the node that is visible will be 1, 2, and 5. Nodes 3 and 4 are hidden by node 2 and node 6 and node 7 are hidden by node 5. To implement this we will do level order traversal using the Breadth First Search algorithm. For each level we will start from the right side and keep updating a variable at the end of each level the value of the variable will be the leftmost value.

Level 1: Iterate Node 1, no node on left move to next level. Node 1 is visible at this level in the left view.

Level 2: Start iterating node 4 and update the value of the variable. Move to node 3 and update the variable value and then move to node 2. Node 2 is visible at this level in the left view.

Level 3: Start with node 7 and update the value of the variable. Move to node 6 and update the variable value and then move to node 5. Node 5 is visible at this level in the left view.

### Example

In this code, we have implemented a queue data structure and its functions as well as currently there is no pre − build library for queues in Golang.

package main

import "fmt"

type Queue struct {
List [](*TreeNode)
}

type TreeNode struct {
Val   int
Left  *TreeNode
Right *TreeNode
}

// function to add an element in the queue
func (q *Queue) Enqueue(element *TreeNode) {
q.List = append(q.List, element)
}

// function to delete elements in the queue
func (q *Queue) Dequeue() *TreeNode {
if q.isEmpty() {
fmt.Println("Queue is empty.")
return nil
}
element := q.List[0]
q.List = q.List[1:]

return element
}

// function checks that queue is empty or not
func (q *Queue) isEmpty() bool {
return len(q.List) == 0
}

// function to find the length of the queue
func (q *Queue) size() int {
return len(q.List)
}

// creating binary tree
func CreateBinaryTree(root *TreeNode) {
n1 := TreeNode{1, nil, nil}
n2 := TreeNode{2, nil, nil}
root.Left = &n1
root.Right = &n2

n3 := TreeNode{3, nil, nil}
n4 := TreeNode{4, nil, nil}
n1.Left = &n3
n1.Right = &n4

n5 := TreeNode{5, nil, nil}
n6 := TreeNode{6, nil, nil}
n2.Left = &n5
n2.Right = &n6
}

// LeftView a function with root node as argument
// and returns the left-view elements in the array
func LeftView(root *TreeNode) []int {
// returning empty array if the tree is empty
if root == nil {
return []int{}
}

// creating variable for queue
var q Queue

// creating array to store right side element
var leftView []int

// variable to store right most value at the current level
var Val int

// enqueue root address in the queue
q.Enqueue(root)
q.Enqueue(nil)

// breadth-first search over the tree
for q.size() > 1 {
currNode := q.Dequeue()
if currNode == nil {
q.Enqueue(nil)
leftView = append(leftView, Val)
continue
}
Val = currNode.Val

if currNode.Right != nil {
q.Enqueue(currNode.Right)
}

if currNode.Left != nil {
q.Enqueue(currNode.Left)
}

}
leftView = append(leftView, Val)

return leftView
}

func main() {
fmt.Println("Golang program to find the Left view of the binary tree.")

// creating root node of binary tree
root := TreeNode{0, nil, nil}
// calling CreateBinaryTree function to create a complete binary tree
CreateBinaryTree(&root)

// calling RightView function
leftView := LeftView(&root)

// print right view element
for i := 0; i < len(leftView); i++ {
fmt.Print(leftView[i], " ")
}
fmt.Println()
}


### Output

Golang program to find the Left view of the binary tree.
0 1 3


## Conclusion

In this way, we have found the left view of a binary tree by doing the level order traversal, using breadth first search algorithm. We can use the Depth First search algorithm also to find the level order traversal of a tree. The time complexity of this approach is O(V + E) where V and E are the no. of vertices and no. of edges in the graph. To learn more about Golang you can explore these tutorials.

Updated on: 10-Jul-2023

69 Views