Created
July 9, 2017 09:21
-
-
Save anil477/e4d57b6f23faecdb2411c2cfe2fadabe to your computer and use it in GitHub Desktop.
Symmetric Tree (Mirror Image of itself).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java program to check is binary tree is symmetric or not | |
class Node | |
{ | |
int key; | |
Node left, right; | |
Node(int item) | |
{ | |
key = item; | |
left = right = null; | |
} | |
} | |
class BinaryTree | |
{ | |
Node root; | |
// returns true if trees with roots as root1 and root2 are mirror | |
boolean isMirror(Node node1, Node node2) | |
{ | |
// if both trees are empty, then they are mirror image | |
if (node1 == null && node2 == null) | |
return true; | |
// For two trees to be mirror images, the following three | |
// conditions must be true | |
// 1 - Their root node's key must be same | |
// 2 - left subtree of left tree and right subtree | |
// of right tree have to be mirror images | |
// 3 - right subtree of left tree and left subtree | |
// of right tree have to be mirror images | |
if (node1 != null && node2 != null && node1.key == node2.key) | |
return (isMirror(node1.left, node2.right) | |
&& isMirror(node1.right, node2.left)); | |
// if neither of the above conditions is true then | |
// root1 and root2 are mirror images | |
return false; | |
} | |
// returns true if the tree is symmetric i.e | |
// mirror image of itself | |
boolean isSymmetric(Node node) | |
{ | |
// check if tree is mirror of itself | |
return isMirror(root, root); | |
} | |
// Driver program | |
public static void main(String args[]) | |
{ | |
BinaryTree tree = new BinaryTree(); | |
tree.root = new Node(1); | |
tree.root.left = new Node(2); | |
tree.root.right = new Node(2); | |
tree.root.left.left = new Node(3); | |
tree.root.left.right = new Node(4); | |
tree.root.right.left = new Node(4); | |
tree.root.right.right = new Node(3); | |
boolean output = tree.isSymmetric(tree.root); | |
if (output == true) | |
System.out.println("1"); | |
else | |
System.out.println("0"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment