Binary Tree Visualizer

← Back to Home

Controls

Traversals

Current Tree Array

Traversal Output

Preorder Traversal (Binary Tree)

Steps:

  1. Start at the root node of the tree.
  2. Visit the current node and process its value.
  3. Recursively traverse the left subtree in preorder.
  4. Recursively traverse the right subtree in preorder.
  5. Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(h)

Inorder Traversal (Binary Tree)

Steps:

  1. Start at the root node of the tree.
  2. Recursively traverse the left subtree in inorder.
  3. Visit the current node and process its value.
  4. Recursively traverse the right subtree in inorder.
  5. Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(h)

Postorder Traversal (Binary Tree)

Steps:

  1. Start at the root node of the tree.
  2. Recursively traverse the left subtree in postorder.
  3. Recursively traverse the right subtree in postorder.
  4. Visit the current node and process its value.
  5. Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(h)

Level Order Traversal (Binary Tree)

Steps:

  1. Start at the root node of the tree.
  2. Initialize a queue and enqueue the root node.
  3. While the queue is not empty:
    1. Dequeue a node from the front of the queue and process its value.
    2. If the dequeued node has a left child, enqueue it.
    3. If the dequeued node has a right child, enqueue it.
  4. Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(n)

Tree Visualization