java recursive 10 lines


  • 0
    H
    public class Solution {
        public boolean isSymmetric(TreeNode root) {
            if(root == null) return true;
            return isSymmetricHelper(root.left, root.right);
        }
        private boolean isSymmetricHelper(TreeNode left, TreeNode right){
            if(left == null && right == null) return true;
            if(left == null ^ right == null) return false;
            return left.val == right.val && isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
        }
    }
    

Log in to reply
 

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