Ctrl K

Left, Node, Right

Easy
You are given the root of a binary tree. List the values of all its nodes in this order: for every node, first its entire left subtree, then the node itself, then its entire right subtree. The rule applies to each subtree just as it does to the whole tree. If you draw the tree and project the nodes onto a horizontal line, you get exactly this order, left to right. Return the list of values. For an empty tree, return an empty list. *The recursive version takes a couple of lines, so try doing it without recursion.*
Example 1
root=tree_node([6, 3, 9, 1, 4, None, 12]) → [1, 3, 4, 6, 9, 12]

First the whole left subtree of the root (1, 3, 4), then the root 6 itself, then the right subtree (9, 12).

Example 2
root=tree_node([7, None, 5, 8]) → [7, 8, 5]

7 has no left child, so it comes first. Then its right subtree: 5 has a left child 8, which goes before 5.

Example 3
root=tree_node([]) → []

An empty tree gives an empty list.

Constraints
  • 0 ≤ number of nodes ≤ 1000
  • -1000 ≤ Node.val ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsParent Before Children EasyChildren Before Parent EasySearch Tree Into a Line EasyTree Into a Right Chain MediumK Keys Nearest to a Target Hard
loading Python…
Input
root=tree_node([6, 3, 9, 1, 4, None, 12])
Expected
[1, 3, 4, 6, 9, 12]

Run checks the first 3 tests in your browser. Submit checks all 84 tests on the server, hidden ones included.