Home Binary Trees - Reverse level order traversal
Post
Cancel

Binary Trees - Reverse level order traversal

Question

Write an algorithm that traverses the binary tree in reverse order from the bottom level to the top level. Consider the example:

flowchart TD
   2((2)) --> 7((7))
   7((7)) --> 4((4))
   7((7)) --> 6((6)) 
   2((2)) --> 9((9))

Expected output 4-6-7-9-2.

The solution leverages a stack to reverse the traversal performed by the breadth-first search of the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from queue import Queue

class Node:

    def __init__(self, value) -> None:
        self.value = value 
        self.right = None 
        self.left = None 


def reverseLevelOrder(root):

    if not root:
        return 

    stack = []
    queue = Queue() 

    queue.put(root)

    while not queue.empty():

        top = queue.get()
        stack.append(top)

        # Add right child before left
        if top.right:
            queue.put(top.right)
        if top.left:
            queue.put(top.left)

    # Once queue is exhausted we pop off all elements in stack and concatenate
    result = ""

    while stack:
        top = stack.pop()
        result += f"{top.value}-"

    return result
This post is licensed under CC BY 4.0 by the author.