Simple Recursive approach in Java


#1

/**

  • Definition for binary tree

  • class TreeNode {

  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) {
    
  •  val = x;
    
  •  left=null;
    
  •  right=null;
    
  • }
    
  • }
    */
    public class Solution {
    public int isSymmetric(TreeNode A) {

     boolean b = fun(A.left,A.right);
     if(b)
         return 1;
     return 0;
    

    }

    public boolean fun(TreeNode t1,TreeNode t2){

     if(t1 == null && t2 == null)
         return true;
     if(t1 == null || t2 == null)
         return false;
     if(t1.val == t2.val){
         
           boolean b1 = fun(t1.left,t2.right);
     boolean b2 = fun(t1.right,t2.left);
     
     if(b1 && b2)
         return true;
     //return false;
     }
     
     return false;
    

    }
    }