Question: Given a binary tree, find out if the tree can be folded or not.
A tree can be folded if left and right subtrees of the tree are structure wise mirror image of each other. An empty tree is considered as foldable.
Consider the below trees: (a) and (b) can be folded. (c) and (d) cannot be folded. (a) 10 / \ 7 15 \ / 9 11 (b) 10 / \ 7 15 / \ 9 11 (c) 10 / \ 7 15 / / 5 11 (d) 10 / \ 7 15 / \ / 9 10 12
Method 1 (Change Left subtree to its Mirror and compare it with Right subtree)
1) If tree is empty, then return true. 2) Convert the left subtree to its mirror image mirror(root->left); /* See this post */ 3) Check if the structure of left subtree and right subtree is same and store the result. res = isStructSame(root->left, root->right); /*isStructSame() recursively compares structures of two subtrees and returns true if structures are same */ 4) Revert the changes made in step (2) to get the original tree. mirror(root->left); 5) Return result res stored in step 2.
Thanks to ajaym for suggesting this approach.
// C# program to check foldable
// binary tree
/* A binary tree node has data,
pointer to left child and
a pointer to right child */
public int data;
public Node left, right;
public Node(int item)
data = item;
left = right = null;
/* Returns true if the given
tree is foldable */
Boolean isFoldable(Node node)
/* base case */
if (node == null)
/* convert left subtree
to its mirror */
/* Compare the structures of the
right subtree and mirrored
left subtree */
res = isStructSame(node.left, node.right);
/* Get the originial tree back */
Boolean isStructSame(Node a, Node b)
if (a == null && b == null)
if (a != null && b != null &&
isStructSame(a.left, b.left) &&
/* UTILITY FUNCTIONS */
/* Change a tree so that the roles
of the left and right pointers are
swapped at every node.
for details */
void mirror(Node node)
if (node == null)
/* do the subtrees */
/* swap the pointers in this node */
temp = node.left;
node.left = node.right;
node.right = temp;
// Driver Code
static public void Main(String args)
BinaryTree tree = new BinaryTree();
/* The constructed binary tree is
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.right.left = new Node(4);
tree.root.left.right = new Node(5);
Console.WriteLine(“tree is foldable”);
Console.WriteLine(“Tree is not foldable”);
// This code is contributed by Arnab Kundu
tree is foldable
Time complexity: O(n)
Method 2 (Check if Left and Right subtrees are Mirror)
There are mainly two functions:
// Checks if tree can be folded or not
IsFoldable(root) 1) If tree is empty then return true 2) Else check if left and right subtrees are structure wise mirrors of each other. Use utility function IsFoldableUtil(root->left, root->right) for this.
// Checks if n1 and n2 are mirror of each other.
IsFoldableUtil(n1, n2) 1) If both trees are empty then return true. 2) If one of them is empty and other is not then return false. 3) Return true if following conditions are met a) n1->left is mirror of n2->right b) n1->right is mirror of n2->left
Thanks to Dzmitry Huba for suggesting this approach.
Please write comments if you find the above code/algorithm incorrect, or find other ways to solve the same problem.
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Enumeration of Binary Trees
- Check if leaf traversal of two Binary Trees is same?
- Data Structures | Binary Trees | Question 15
- Count Balanced Binary Trees of Height h
- Data Structures | Binary Trees | Question 1
- Self-Balancing-Binary-Search-Trees (Comparisons)
- Data Structures | Binary Trees | Question 14
- Data Structures | Binary Trees | Question 6
- Data Structures | Binary Trees | Question 13
- Data Structures | Binary Trees | Question 12
- Data Structures | Binary Trees | Question 11
- Data Structures | Binary Trees | Question 10
- Data Structures | Binary Trees | Question 9
- Find first non matching leaves in two binary trees
Improved By : andrew1234