23. Left View of Binary Tree
The problem can be found at the following link: Question Link
Problem Description
Note: Sorry for uploading late; my exam is going on.
Given a Binary Tree, return the Left view of it. The Left view of a Binary Tree is the set of nodes visible when the tree is visited from the Left side. The task is to complete the function leftView(), which accepts the root of the tree as an argument. If no left view is possible, return an empty list.
Example:
Input:
1
/ \
3 2Output:
1 3Input:
10
/ \
20 30
/ \
40 60Output:
My Approach
Recursive Traversal:
Use a recursive function to traverse the tree and keep track of the maximum level visited so far.
If the current level is greater than the maximum level seen so far, add the current node’s value to the result.
Traverse the left subtree before the right subtree to ensure the leftmost node at each level is considered first.
Utility Function:
The utility function
leftViewUtilis used to perform the actual traversal, updating the result list when a node at a new level is encountered.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), as each node is visited exactly once.
Expected Auxiliary Space Complexity: O(N), considering the recursive stack space used during the traversal.
Code (C++)
Code (Java)
Code (Python)
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated