Recursive Java Solution without auxiliary function


  • 0
    Y
    public boolean isSymmetric(TreeNode root) {
            if(root == null)return true;
            if(root.left == null && root.right == null){
                return true;
            }else if(root.left == null || root.right == null){
                return false;
            }else if(root.left.val != root.right.val)return false;
    
            TreeNode temp = (root.left.right != null) ? root.left.right : null;
            root.left.right = (root.right.right != null) ? root.right.right : null;
            root.right.right = temp;
            return (isSymmetric(root.left) && isSymmetric(root.right));
        }
    

Log in to reply
 

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