| 17 | return checkMirror(root.left, root.right); |
| 18 | } |
| 19 | public boolean checkMirror(TreeNode lt, TreeNode rt){ |
| 20 | /* if root's left and root's right both are null, is then that subtree is symmetric */ |
| 21 | if(lt==null && rt==null) |
| 22 | return true; |
| 23 | |
| 24 | /* if root's left is null and root's right is not null and vice-versa, is then that subtree is not symmetric */ |
| 25 | if((lt!=null && rt==null) || (lt==null && rt!=null)) |
| 26 | return false; |
| 27 | |
| 28 | /* check if left node's value matches with right node's value, and recursively check for their left and right subtrees */ |
| 29 | if(lt.val==rt.val && checkMirror(lt.left,rt.right) && checkMirror(lt.right,rt.left)) |
| 30 | return true; |
| 31 | |
| 32 | return false; |
| 33 | } |
| 34 | } |