DSA SectionMedium

Foldable Tree

Detailed guide and Python implementation for the 'Foldable Tree' problem.

Problem Statement

Medium

Write a function is_foldable(tree_arr) that takes an array representation of a binary tree tree_arr and checks if the tree is foldable. A tree is foldable if its left and right subtrees are structural mirrors of each other (value does not matter, only structure).

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

Examples

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

Left child has right child 4. Right child has left child 5. Structures are symmetric mirrors.

Example 2
Input
tree_arr = [1, 2, 3, 4, None, 5, None]
Output
False
Need a Hint?
Consider using Trees-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.