# Check whether Binary Tree is foldable or not.

### Given a binary tree, Check if tree can be folded or not?Java Program to Check whether Tree is foldable.

Check whether given binary tree can be folded or not.

Lets see what is the input and expected output for better understanding on Foldable Binary Tree.

### Algorithm

Binary Tree is said to be Foldable if nodes of Left and Right Subtree are exact mirror of each other.

Step 1: Traverse Binary Tree and compare left node of Left subtree with right node of Right subtree.

Step 2: Traverse Binary Tree and compare right node of Left subtree with left node of Right subtree.

Step 3:
In both steps 1 and 2 above, check both left and right node is null or both left and right node is not null, if Yes, then Tree is foldable and has identitical structure otherwise not.

Note: To check whether Tree is Foldable, we will not be looking at data of the nodes and concentrate only on structure of Tree.

Algorithm to check whether Tree is foldable is very similar to algorithm for Checking whether Binary Trees are Mirror of each other.

### Java Program to Check whether Tree is foldable.

```public class FoldableBinaryTree {

public static void main(String[] args) {
new FoldableBinaryTree();
}

public FoldableBinaryTree(){
Node rootNode=null;

System.out.println(isFoldableBinaryTree(rootNode));
}

private boolean isFoldableBinaryTree(Node rootNode) {
if(rootNode==null){
return true;
}
return isFoldableBinaryTreeUtil(rootNode.getLeft(), rootNode.getRight());
}

private boolean isFoldableBinaryTreeUtil(Node rootNodeLeft, Node rootNodeRight) {

//Check both left and right node is null, if yes then that is fine, return true.
if(rootNodeLeft == null && rootNodeRight == null){
return true;
}

//Control reach to this point only in 2 conditions,
//1. Either of left or right node is null (not both as we did that check above).
//2. left and right both node is not null. (that is fine)
//So if we found either of left or right node is null, we return false;(check point 1 in above case)
if(rootNodeLeft == null || rootNodeRight == null){
return false;
}

boolean left = isFoldableBinaryTreeUtil(rootNodeLeft.getLeft(), rootNodeRight.getRight());
boolean right = isFoldableBinaryTreeUtil(rootNodeLeft.getRight(), rootNodeRight.getLeft());

//If both of Left and Right subtree return true then only return true else return false.
return left && right;
}

private static Node addNode(Node rootNode, int i, boolean isRootNode) {
if(rootNode==null){
return new Node(i);
}else{
if(i > rootNode.getData()){
if(isRootNode){
}else{
}

}else{
if(isRootNode){
}else{
}
}
}
return rootNode;
}
}

```
Node.java
```package tree;

public class Node{
private Node left;
private Node right;
private int data;

public Node(int data){
this.data=data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}

```