Data Structures · Visualization

Binary Tree: Delete

Binary Tree

Deleting a node from a general binary tree (keeping it complete/balanced in shape) involves three steps: find the node to delete, find the very last node in level order, copy that last node's value into the node to delete, and then remove the last node. Time: O(n) to locate both the target and the last node. Space: O(n) worst case for the traversal queue/stack.

Big-O: Traversals visit every node exactly once: O(n).

Binary Tree: Delete

Binary Tree

Deleting a node from a general binary tree (keeping it complete/balanced in shape) involves three steps: find the node to delete, find the very last node in level order, copy that last node's value into the node to delete, and then remove the last node. This keeps the tree's shape compact.

How Delete Works

  1. Find the node containing the target value using a traversal.
  2. Find the deepest, rightmost node in the tree (the last node in level order).
  3. Copy the last node's value into the target node.
  4. Remove the last node from its original position.

Delete Worked Example

Input: Delete 5 from root(10) -> left(5), right(7)

  1. Target node found: 5 (root's left child)
  2. Deepest rightmost node is 7
  3. Copy 7's value into the target node: root.left becomes 7
  4. Remove the original node 7

Output: 10 / \ 7 (empty)

Try your own input
Binary Tree: Introduction
A hierarchy of nodes where each node has at most two children — usually called "left" and "right".
45
20
10
30
40
65
50
60
70
80
Active Result
Binary Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Binary Trees in Code
def delete(root, val):
    if not root:
        return None
    if val < root.val:
        root.left = delete(root.left, val)
    elif val > root.val:
        root.right = delete(root.right, val)
    else:  # found
        if not root.left:
            return root.right
        if not root.right:
            return root.left
        # two children: replace with successor
        succ = root.right
        while succ.left:
            succ = succ.left
        root.val = succ.val
        root.right = delete(root.right, succ.val)
    return root
# example: delete 5
Input: Delete 5 from root(10) -> left(5), right(7)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Delete Time & Space Complexity

TimeO(n) to locate both the target and the last node
SpaceO(n) worst case for the traversal queue/stack

When to Use Delete

  • Removing an item from a complete-tree-based structure like a heap
  • Maintaining a compact tree shape after removals
  • General tree maintenance where order does not matter, only structure

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.