The problem. Given the root of a binary tree, return its left view: the values you would see standing to the left of the tree — the first (leftmost) node of every level, from top to bottom.
Input: root = [1, 2, 3, 4, 5, 6, 7, null, 8] Output: [1, 2, 4, 8]
Input: root = [1, null, 2, null, 3] Output: [1, 2, 3]
Input: root = [] Output: []It's tempting to just follow left children down from the root — but that's wrong: on a level where the left side has run out, the leftmost node may be a right child, deep in the right subtree. The second example has no left children at all. What we need is the first node of each level.
Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson