Back to Practice Dashboard
DSA SectionMedium

Tree Traversals

Learn how to solve the 'Tree Traversals' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Medium

Write a function get_tree_traversals(tree_arr) that takes an array representation of a binary tree tree_arr and returns a list of lists containing [inorder, preorder, postorder] traversals of the tree.

Constraints
  • 0 <= len(tree_arr) <= 1000

Examples

Example 1
Input
tree_arr = [1, None, 2, None, None, 3]
Output
[[1, 3, 2], [1, 2, 3], [3, 2, 1]]
Explanation

Inorder traversal visits left-root-right: [1, 3, 2]. Preorder visits root-left-right: [1, 2, 3]. Postorder visits left-right-root: [3, 2, 1].

Need a Hint?
Perform a recursive tree traversal (DFS) or level-order traversal (BFS) using a queue/stack.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor