Easy and simple using one queue iterative in java


  • 17
    C
    public class Solution {
        public boolean isSymmetric(TreeNode root) {
            if(root == null) return true;
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.offer(root.left);
            queue.offer(root.right);
            while(!queue.isEmpty()){
                TreeNode left = queue.poll();
                TreeNode right = queue.poll();
                if(left == null && right == null) continue;
                if(left == null || right == null) return false;
                if(left.val != right.val) return false;
                queue.offer(left.left);
                queue.offer(right.right);
                queue.offer(left.right);
                queue.offer(right.left);
                
            }
            return true;
            
        }
    }

  • 0
    Y

    必须赞一个!这个思路非常清晰,感觉好像打开了另一个视野~~~~~~~~~~~~~~~~~~


  • 0
    A

    Nice! thanks for sharing


Log in to reply
 

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