Symmetric Tree
Total Accepted: 13819 Total Submissions: 43667
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3Note:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSymmetric(TreeNode root) { if(root == null){ return true; } return bfs(root); } public boolean bfs(TreeNode root){ Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); while(!queue.isEmpty()){ int size = queue.size(); List<Integer> numList = new ArrayList<Integer>(); while(size > 0){ size--; TreeNode node = queue.peek(); queue.poll(); if(node.left != null){ numList.add(node.left.val); queue.offer(node.left); }else{ numList.add(-1); } if(node.right != null){ numList.add(node.right.val); queue.offer(node.right); }else{ numList.add(-1); } } boolean flag = isSymmetric(numList); if(!flag){ return false; } } return true; } public boolean isSymmetric(List<Integer> numList){ int size = numList.size(); int low = 0; int high = size-1; while(low < high){ if(numList.get(low) != numList.get(high)){ return false; } low++; high--; } return true; } }