/**
* Node Class of Tree
* @author Prateek
*/
class Node {
public Node left;
public int data;
public Node right;
public Node(int val) {
this.data=val;
}
@Override
return this.data + "";
}
}
/**
* Check if Binary Tree is Symmetric
* @author Prateek
*/
class SymmetricBinaryTree {
/**
* Sub-routine to check if Binary Tree is Symmetric
*/
public boolean isSymmetric(Node left, Node right){
if ( (left==null && right!=null) || (left!=null && right==null) )
return false;
if(left==null && right==null)
return true;
return isSymmetric(left.left,right.right) && isSymmetric(left.right,right.left);
}
public static void main
(String[] args
) { SymmetricBinaryTree obj = new SymmetricBinaryTree();
Node root = new Node(1);
root.left = new Node(2);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right = new Node(3);
root.right.left = new Node(6);
root.right.right = new Node(7);
boolean isSymmetric = obj.isSymmetric(root.left,root.right);
System.
out.
println(isSymmetric
); }
}
LyoqCiAqIE5vZGUgQ2xhc3Mgb2YgVHJlZQogKiBAYXV0aG9yIFByYXRlZWsKICovCmNsYXNzIE5vZGUgewoJcHVibGljIE5vZGUgbGVmdDsKCXB1YmxpYyBpbnQgZGF0YTsKCXB1YmxpYyBOb2RlIHJpZ2h0OwoKCXB1YmxpYyBOb2RlKGludCB2YWwpCXsKCQl0aGlzLmRhdGE9dmFsOwoJfQoKCUBPdmVycmlkZQoJcHVibGljIFN0cmluZyB0b1N0cmluZygpewoJCXJldHVybiB0aGlzLmRhdGEgKyAiIjsKCX0KfQoKLyoqCiAqIENoZWNrIGlmIEJpbmFyeSBUcmVlIGlzIFN5bW1ldHJpYwogKiBAYXV0aG9yIFByYXRlZWsKICovCmNsYXNzIFN5bW1ldHJpY0JpbmFyeVRyZWUgewoJLyoqCgkgKiBTdWItcm91dGluZSB0byBjaGVjayBpZiBCaW5hcnkgVHJlZSBpcyBTeW1tZXRyaWMKCSAqLwoJcHVibGljIGJvb2xlYW4gaXNTeW1tZXRyaWMoTm9kZSBsZWZ0LCBOb2RlIHJpZ2h0KXsKCQlpZiAoIChsZWZ0PT1udWxsICYmIHJpZ2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