LeatherDeer

Untitled

Dec 19th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.48 KB | None | 0 0
  1. public boolean isSymmetric(TreeNode root) {
  2.     Queue<TreeNode> q = new LinkedList<>();
  3.     q.add(root);
  4.     q.add(root);
  5.     while (!q.isEmpty()) {
  6.         TreeNode t1 = q.poll();
  7.         TreeNode t2 = q.poll();
  8.         if (t1 == null && t2 == null) continue;
  9.         if (t1 == null || t2 == null) return false;
  10.         if (t1.val != t2.val) return false;
  11.         q.add(t1.left);
  12.         q.add(t2.right);
  13.         q.add(t1.right);
  14.         q.add(t2.left);
  15.     }
  16.     return true;
  17. }
Add Comment
Please, Sign In to add comment