DSA Section

Foldable Tree

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

問題提起

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).

制約
  • 0 <= len(tree_arr) <= 1000

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

解決する準備はできましたか?

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

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。