InferenceAI

Enter at least two characters to search.

Start free

Mirror a Tree

easyTrees15 min10 XP

Mirror the binary tree (swap the left and right child of every node) and return its values level by level, left to right.

Trees are given in level order as a list, with None for a missing child (for example [3, 9, 20, None, None, 15, 7]).

Examples

EXAMPLE WALKTHROUGHfull tree

A guided view of the published example, not a trace of your code.

INPUT[[4,2,7,1,3,6,9]]
TASKmirror_tree()
EXPECTED OUTPUT[[4],[7,2],[9,6,3,1]]

Start with the input values shown in this example.

Example 1 of 3

Input: values = [4,2,7,1,3,6,9]

Output: [[4],[7,2],[9,6,3,1]]

Explanation: full tree

Input: values = [2,1,3]

Output: [[2],[3,1]]

Explanation: small

Input: values = []

Output: []

Explanation: empty

Hint 1

Recursively swap children, or swap while traversing with a queue.

Topics

DSATreesPython