Share my accepted Java solution!


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

Log in to reply
 

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