Print BottomLeft View of a Binary Tree
In this article, we will tackle the problem of printing the bottomleft view of a binary tree. This problem requires us to find and print the leftmost elements at each level of the binary tree when viewed from the bottom. To solve this problem, we will use a depthfirst search (DFS) approach to traverse the tree and keep track of the leftmost elements at each level.
Problem Statement
Given a binary tree, we want to print the bottomleft view of the tree. The bottomleft view of a binary tree is the set of nodes visible when the tree is viewed from the left side at each level. In other words, for each level of the tree, we want to print the leftmost node at that level.
Example
Let's consider a binary tree as an example:
10
/ \
2 4
/ / \
3 6 5
\
7
/ \
8 11
The bottomleft view of this tree is [3, 6, 8, 11]
when viewed from the left side at each level, as
indicated by the arrows in the tree diagram.
Idea to Solve the Problem
To print the bottomleft view of the binary tree, we can perform a depthfirst traversal of the tree while maintaining a record of the leftmost node at each level. We will use an unordered map to store this information, where the key will represent the level of the node, and the value will be the leftmost node's value at that level. We'll start the traversal from the root node with a distance of 0 and increment the distance as we move to the right child and stay at the same distance when moving to the left child.
Pseudocode
Here is the pseudocode for solving the problem:
function bottomLeftView(node, distance, record):
if node is not null:
record[distance] = node.data
bottomLeftView(node.right, distance + 1, record)
bottomLeftView(node.left, distance, record)
function printBottomLeft():
record = empty unordered map
bottomLeftView(root, 0, record)
distance = 0
while distance < size of record:
print record[distance]
increment distance by 1
Algorithm Explanation
 We start by initializing an empty unordered map called
record
to store the leftmost nodes at each level.  We call the
bottomLeftView
function starting from the root node with an initial distance of 0.  In the
bottomLeftView
function: If the current node is not null, we update the
record
with the node's data at the current distance.  Then, we recursively call the function for the right child with an increased distance (moving right).
 We also recursively call the function for the left child without changing the distance (moving left).
 If the current node is not null, we update the
 After the traversal is complete, we iterate through the
record
map, starting from distance 0 and printing the leftmost node's value at each level.  We increment the distance by 1 in each iteration until we have printed all the leftmost nodes at each level.
Code Solution

1) Bottom left view of binary tree in c++
2) Bottom left view of binary tree in java
3) Bottom left view of binary tree in c#
4) Bottom left view of binary tree in vb.net
5) Bottom left view of binary tree in php
6) Bottom left view of binary tree in node js
7) Bottom left view of binary tree in python
8) Bottom left view of binary tree in ruby
9) Bottom left view of binary tree in scala
10) Bottom left view of binary tree in swift
11) Bottom left view of binary tree in kotlin
12) Bottom left view of binary tree in golang
Time Complexity of the Code
The time complexity of this code is O(N), where N is the number of nodes in the binary tree. This is because we visit each node once during the depthfirst traversal, and the map operations take constant time.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment