Left View of a Binary Tree
Updated
Problem statement
Given the root of a binary tree, return its left view as a list of node values from top to bottom.
At each depth, include the leftmost existing node when the nodes at that depth are ordered from left to right. Include one value for every nonempty depth, even when values repeat.
An empty tree returns an empty list. The tree is displayed in level order, using null for missing children.
Function
leftView(root: TreeNode) → List<Integer>Examples
Example 1
root = [1,2,3,null,4,5,6]return = [1,2,4]The first nodes at depths 0, 1, and 2 have values 1, 2, and 4.
Example 2
root = [8,null,9,null,10]return = [8,9,10]There is one existing node at each depth. All three are visible despite having only right-child links.
Constraints
- The tree has
0to5000nodes. - Each node value is an integer in
[-10^6, 10^6]. - Each node has at most two children, and the input is a valid tree with no cycles or shared child nodes.
More Mygate Phone Screen questions
- Employees Earning More Than Their ManagerPHONE SCREEN · Seen Sep 2026
- Maximum Subarray SumPHONE SCREEN · Seen Sep 2026
- Reverse a Linked ListPHONE SCREEN · Seen Sep 2026
- Users Active in Both January and FebruaryPHONE SCREEN · Seen Sep 2026
- Elements Occurring More Than n/3 TimesPHONE SCREEN · Seen Jan 2025
- Frog Jump: Minimum EnergyPHONE SCREEN · Seen Jan 2025
- Two SumPHONE SCREEN · Seen Aug 2024
- Minimum in a Rotated Sorted ArrayPHONE SCREEN · Seen Jul 2024
- Minimum Subarray SumPHONE SCREEN · Seen Sep 2023
- Remove the Nth Node from the EndPHONE SCREEN · Seen Sep 2023
Similar questions at other companies
- Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Mirror a Binary TreeAdobe · PHONE SCREENAdobe interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Distance Between Two Nodes in a Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions