Controls
Traversals
Current Tree Array
Traversal Output
Preorder Traversal (Binary Tree)
Steps:
- Start at the root node of the tree.
- Visit the current node and process its value.
- Recursively traverse the left subtree in preorder.
- Recursively traverse the right subtree in preorder.
- Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(h)
Inorder Traversal (Binary Tree)
Steps:
- Start at the root node of the tree.
- Recursively traverse the left subtree in inorder.
- Visit the current node and process its value.
- Recursively traverse the right subtree in inorder.
- Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(h)
Postorder Traversal (Binary Tree)
Steps:
- Start at the root node of the tree.
- Recursively traverse the left subtree in postorder.
- Recursively traverse the right subtree in postorder.
- Visit the current node and process its value.
- Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(h)
Level Order Traversal (Binary Tree)
Steps:
- Start at the root node of the tree.
- Initialize a queue and enqueue the root node.
- While the queue is not empty:
- Dequeue a node from the front of the queue and process its value.
- If the dequeued node has a left child, enqueue it.
- If the dequeued node has a right child, enqueue it.
- Continue until all nodes are visited.
Time Complexity: O(n)
Space Complexity: O(n)