# symmetric binary tree

We need to write a recursive function isSymmetrical() that takes two trees as argument and returns true if trees are Symmetrical and false if trees are not Symmetrical. The isSymmetrical() function recursively checks two roots and subtrees under the root.

The Code is –

```class Node {
int data;
Node left, right;

public Node(int item) {
data = item;
left = right = null;
}
public Node() {
data = 0;
left = right = null;
}
}

// Binary tree Class
class BTree {

static Node root;
static boolean isSymmetryTree(Node node) {
if (node == null) {
return true;
}
return isSymmetry(node.left, node.right);
}

static boolean isSymmetry(Node nodeLeft, Node nodeRight) {

//Check both left and right node is null, if yes then that is fine, return true.
if (nodeLeft == null && nodeRight == null) {
return true;
}
//If one is present and other is null, return false.
if (nodeLeft == null || nodeRight == null) {
return false;
}
if (nodeLeft.data != nodeRight.data)
return false;
// The most Important step is -> when you are Checking if it is symmetric ,
// Send the left child of left subtree and right child of right subtree together. Similarly,
// send the right child of left subtree and left child of right subtree.
boolean left = isSymmetry(nodeLeft.left, nodeRight.right);
boolean right = isSymmetry(nodeLeft.right, nodeRight.left);

return (left && right);
}

public static void main(String[] args) {
BTree tree = new BTree();
tree.root = new Node(1);
tree.root.left = new Node(5);
tree.root.right = new Node(4);
tree.root.left.left = new Node(9);
tree.root.left.right = new Node(7);
tree.root.right.left = new Node(10);
tree.root.right.right = new Node(13);
System.out.println(" The Symmetry of tree is " + tree.isSymmetryTree(root));

}
}
```