Simple Java iterative using two queue, 3ms

• ``````    public boolean isSymmetric(TreeNode root) {

if(null == root || null == root.left && null == root.right) return true;
while(leftq.size() != 0 && rightq.size() != 0){
TreeNode left = leftq.poll();
TreeNode right = rightq.poll();
if(null == left && null == right) continue;//if both null node.
if(null == left || null == right || left.val != right.val) return false;

}
return leftq.size() == rightq.size();
}``````

• I think I need to add parentheses in the first if sentence to make it clearer. Like below :

``if(null == root || (null == root.left && null == root.right)) return true;``

• My recursive solution, only takes 1 ms.

``````public class Solution {
public boolean isSymmetric(TreeNode root) {
return symmetric(root, root);
}

private boolean symmetric(TreeNode leftRoot, TreeNode rightRoot){
if(null == leftRoot && null == rightRoot) return true;
if(null == leftRoot || null == rightRoot || leftRoot.val != rightRoot.val) return false;

return symmetric(leftRoot.left, rightRoot.right) && symmetric(leftRoot.right, rightRoot.left);
}
``````

}