Problem Statement:
Check if the Given Binary Tree is Symmetric about the plane passing through root.
Solution:
Full Source Code : LINK
Below is the recursive implementation to check if Binary Tree is Symmetric
Please comment and post your suggestions.
Happy Coding!! :)
Check if the Given Binary Tree is Symmetric about the plane passing through root.
Fig: Symmetric Binary Tree about the place passing through root |
Solution:
Full Source Code : LINK
Below is the recursive implementation to check if Binary Tree is Symmetric
1 2 3 4 5 6 7 8 9 10 11 12 | /** * 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); } |
Please comment and post your suggestions.
Happy Coding!! :)
public boolean isSymmetric(Node left, Node right){
ReplyDeleteif ( (left==null && right!=null) || (left!=null && right==null) || left.data!=right.data )
return false;
if(left==null && right==null && right.data==left.data)
return true;
return isSymmetric(left.left,right.right) && isSymmetric(left.right,right.left);
}
Shouldn't it be added another check?
Thanks for suggesting, you may be correct , but in this problem i am just concerned about the structure of binary tree and not the content of nodes. As u can see i have shown the above tree to be symmetric.
DeleteThanks again fren , keep suggesting.