My AC Code ,is there a better method ?

  • 10
     public boolean checkSymmetric(TreeNode lsubTree,TreeNode rsubTree){
        if(lsubTree==null&&rsubTree==null) return true;
        else if(lsubTree!=null&&rsubTree==null) return false;
        else if(lsubTree==null&&rsubTree!=null) return false;
        else if(lsubTree.val!=rsubTree.val) return false;
        boolean lt=checkSymmetric(lsubTree.left,rsubTree.right);
        boolean rt=checkSymmetric(lsubTree.right,rsubTree.left);
        return lt&&rt;
    public boolean isSymmetric(TreeNode root) {
        if(root==null) return true;
        return checkSymmetric(root.left,root.right);

  • 0

    Other than recursive solution, you can do iterative both left child first and right child first in-order traversal at the same time (two stacks, same loop). The performance should be better than your solution.

  • 0
    This post is deleted!

  • 0

    Why don't you use pointer to pass the parameter? I think it's going to cost other memory space which although will be deconstructed after the function.

  • 0

    @Rudy_Zhang It is a Java solution. There are no pointers in Java.

Log in to reply

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