Print left view of a binary tree
The problem involves printing the left view of a binary tree. The left view of a binary tree consists of all the nodes that are visible when looking at the tree from the left side, starting from the root and proceeding to the deepest level.
Problem Statement and Description
Given a binary tree, the task is to write a program to print the nodes that are visible from the left side of the tree. This means that for each level of the tree, only the leftmost node at that level should be considered for printing.
Example
Consider the following binary tree:
1
/ \
2 3
/ / \
4 5 6
/ \
7 8
/
9
The left view of this tree is [1, 2, 4, 7, 9]. These are the nodes that would be visible when looking at the tree from the left side.
Idea to Solve the Problem
To solve this problem, we can follow these steps:
 Traverse the binary tree using DepthFirst Search (DFS).
 During the traversal, for each level, only consider the first node encountered (leftmost node) and print its value.
 Maintain a variable (
level
) to keep track of the current level.  If the
level
is higher than the previous highest level, print the node's value and update thelevel
.  Recursively visit the left subtree first, then the right subtree.
Standard Pseudocode
Here's a highlevel pseudocode representation of the algorithm to print the left view of a binary tree:
PrintLeftView(node, depth):
if node is NULL:
return
if depth is greater than current level:
print node's value
update current level
Recursively visit left subtree with increased depth
Recursively visit right subtree with increased depth
Algorithm Explanation
 Traverse the binary tree in a DepthFirst Search (DFS) manner.
 For each node encountered, if the current
depth
is greater than thelevel
, print the node's value and update thelevel
.  Recursively visit the left subtree with an increased depth.
 Recursively visit the right subtree with an increased depth.
Code Solution

1) Print left view of binary tree using recursion in java
2) Print left view of binary tree using recursion in c++
3) Print left view of binary tree using recursion in c#
4) Print left view of binary tree using recursion in c
5) Print left view of binary tree using recursion in ruby
6) Print left view of binary tree using recursion in python
7) Print left view of binary tree using recursion in php
8) Print left view of binary tree using recursion in node js
9) Print left view of binary tree using recursion in scala
10) Print left view of binary tree using recursion in swift
11) Print left view of binary tree using recursion in kotlin
12) Print left view of binary tree using recursion in vb.net
13) Print left view of binary tree using recursion in golang
14) Print left view of binary tree using recursion in typescript
Time Complexity
The time complexity of the provided code is determined by the DFS traversal of the binary tree. In the worst case, where the binary tree is completely unbalanced (resembling a linked list), the algorithm's time complexity is O(n), where 'n' is the number of nodes in the tree.
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