Skip to the content
PyVisualizer

Learn it by watching it run

Python binary tree visualizer

A binary tree is nodes linked downward: each node has a value, a left child and a right child. Almost every tree algorithm is a short recursive function, and the trick is seeing which node each call is standing on. Here every call is drawn on the tree.

Run the example

Your own codeData structures and recursionFreeNo signup

Nodes, children and None

A tree node in Python is an object with val, left and right. A missing child is None. The tree as a whole is a reference to its root. Trees are usually written level by level, left to right, with null for a gap, the way LeetCode does: [4, 2, 6, 1, 3, 5, 7] is the tree below. PyVisualizer builds the node objects from that list for you.

44226611335577
The tree [4, 2, 6, 1, 3, 5, 7], with the order an inorder traversal visits its nodes.

Traversal: left, node, right

An inorder traversal visits a node's whole left subtree, then the node, then its right subtree. Written recursively it is three lines, and the base case is an empty subtree (None), where there is nothing to do. On a binary search tree, like this one, inorder visits the values in sorted order.

The other two depth-first orders only move the middle line: preorder records the node before its subtrees, postorder after them.

Inorder traversalRuns inorderTraversal(root = [4, 2, 6, 1, 3, 5, 7])
def inorderTraversal(root):
    result = []

    def visit(node):
        if node is None:
            return
        visit(node.left)
        result.append(node.val)
        visit(node.right)

    visit(root)
    return result
Run this example in PyVisualizer Opens in the tool and runs, ready to step through.

Depth: an answer built from the children

Many tree problems are answered bottom-up: ask both children, then combine. The maximum depth of a tree is 1 (the node itself) plus the larger of its children's depths, and an empty tree has depth 0. Each call returns its answer to its parent, so the leaves answer first and the root answers last.

3920157
The tree [3, 9, 20, null, null, 15, 7]: its depth is 3, through 20 to 15 or 7.
Maximum depthRuns maxDepth(root = [3, 9, 20, null, null, 15, 7])
def maxDepth(root):
    if root is None:
        return 0
    left = maxDepth(root.left)
    right = maxDepth(root.right)
    return 1 + max(left, right)
Run this example in PyVisualizer Opens in the tool and runs, ready to step through.

What you see in PyVisualizer

A traversal like this one goes several calls deep, so it opens on the Recursion view: every visit call is a lane labelled with the node it was called on, and you can watch each one reach None and return. Switch to Memory to see the tree itself, with root and node labelled on the nodes they refer to, and result filling in, 1 to 7, beside it.

Common mistakes

  • No check for None. node.left on a missing node raises AttributeError: 'NoneType' object has no attribute 'left'. Handle the empty subtree first.
  • Dropping a child's answer. Calling maxDepth(root.left) without using what it returns.
  • Confusing the input list with the tree. The level-order list is only a way to write the tree down; the code receives the root node, not the list.

Try it yourself

  • Move the append in the traversal above the first visit call to get preorder, and compare the output.
  • Give the depth example a lopsided tree such as [1, 2, null, 3, null, 4] and watch the calls go down one side.

Open PyVisualizer with your own code