Java Solution with loads of conditions


  • 0
    A
    public class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root == null){
                return true;
            }
            else if (root.left == null && root.right == null){
                return true;
            }
            else if ((root.left != null && root.right != null) && (root.left.val == root.right.val )){
                TreeNode left = root.left;
                TreeNode right = root.right;
                return (isSymmetric(left,right));
            }
            return false;
        }
        
        public boolean isSymmetric (TreeNode left, TreeNode right){
            if (left.val != right.val){
                return false;
            }
            if ((left.left == null && right.right == null) && (left.right == null && right.left == null)){
                return true;
            }
            else if ((left.left == null && right.right == null) && !(left.right == null && right.left == null)){
                return isSymmetric(left.right,right.left);
            }
            else if (!(left.left == null && right.right == null) && (left.right == null && right.left == null)){
                return isSymmetric(left.left,right.right);
            }
            else if (((left.left != null && right.right != null) && (left.left.val == right.right.val)) &&
            ((left.right != null && right.left != null) && (left.right.val == right.left.val))){
                return (isSymmetric(left.left,right.right) && isSymmetric(left.right,right.left));
            }
            return false;
        }
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.