Left View of Binary Tree in Kotlin

he left view of a binary tree is the set of nodes that are visible when the tree is viewed from the left side. In other words, it is the first node encountered at each level of the tree when traversing the tree from left to right.

For example, consider the following binary tree:

               1
           /        \
         2          3
      /    \      /    \
    4      5    6     7

The left view of this tree is {1, 2, 4} since these are the nodes visible when the tree is viewed from the left side at levels 0, 1, and 2, respectively.

To obtain the left view of a binary tree, we can perform a level-order traversal of the tree and keep track of the first node encountered at each level. Here’s an outline of the algorithm:

  1. Create an empty queue and add the root node to it.
  2. While the queue is not empty:
    • Get the size of the queue (which represents the number of nodes at the current level).
    • Loop through the nodes at the current level and:
      • If it is the first node at this level, add it to the left view.
      • Add its left and right children to the queue (if they exist).
  3. Return the left view.
Kotlin
class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}
fun leftView(root: TreeNode?): List<Int> {
    val leftView = mutableListOf<Int>()
    if (root == null) {
        return leftView
    }

    val queue = ArrayDeque<TreeNode>()
    queue.addLast(root)

    while (queue.isNotEmpty()) {
        val levelSize = queue.size
        for (i in 0 until levelSize) {
            val node = queue.removeFirst()
            if (i == 0) {
                leftView.add(node.`val`)
            }
            node.left?.let { queue.addLast(it) }
            node.right?.let { queue.addLast(it) }
        }
    }

    return leftView
}

In this code, the leftView function takes a binary tree represented by its root node as input and returns a list of integers representing the left view of the tree. The function first creates an empty list to store the left view, and checks if the root node is null. If the root node is null, the function simply returns the empty list.

Otherwise, the function creates an empty queue and adds the root node to it. It then enters a loop that continues as long as the queue is not empty. At each iteration of the loop, the function gets the size of the queue (which represents the number of nodes at the current level), and loops through the nodes at the current level. For each node, the function checks if it is the first node at this level (i.e., if its index in the queue is 0), and if so, adds its value to the left view list. The function then adds the node’s left and right children to the queue (if they exist). Finally, the function returns the left view list.

Kotlin
class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}
fun leftView(root: TreeNode?): List<Int> {
    val leftView = mutableListOf<Int>()
    if (root == null) {
        return leftView
    }

    val queue = ArrayDeque<TreeNode>()
    queue.addLast(root)

    while (queue.isNotEmpty()) {
        val levelSize = queue.size
        for (i in 0 until levelSize) {
            val node = queue.removeFirst()
            if (i == 0) {
                leftView.add(node.`val`)
            }
            node.left?.let { queue.addLast(it) }
            node.right?.let { queue.addLast(it) }
        }
    }

    return leftView
}
fun main() {
    // create the binary tree
    val root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left?.left = TreeNode(4)
    root.left?.right = TreeNode(5)
    root.right?.left = TreeNode(6)
    root.right?.right = TreeNode(7)
    
    // get the left view of the binary tree
    val leftView = leftView(root)
    
    // print the left view as a comma-separated list
    println("Left view of binary tree: ${leftView.joinToString()}")
}

In this code, the main function creates a binary tree with the same structure as the example I used in my previous answer. It then calls the leftView function to obtain the left view of the tree, and stores the result in a variable called leftView. Finally, the function prints the left view as a comma-separated list using the joinToString function.

Leave a Comment