/** * 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; } LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); LinkedList<TreeNode> childrenQueue = new LinkedList<TreeNode>(); queue.add(root); while (!queue.isEmpty()){ TreeNode node = queue.remove(); if(node==null || node.left==null){ childrenQueue.add(null); }else{ childrenQueue.add(node.left); } if(node==null || node.right==null){ childrenQueue.add(null); }else{ childrenQueue.add(node.right); } if(queue.isEmpty() && !childrenQueue.isEmpty()){ for(int i=0; i<childrenQueue.size(); i++){ if(childrenQueue.get(i)!=null){ queue.add(childrenQueue.get(i)); } if(i<childrenQueue.size()/2){ int j = childrenQueue.size()-i-1; if(childrenQueue.get(i)==null && childrenQueue.get(j)==null){ continue; } if((childrenQueue.get(i)==null && childrenQueue.get(j)!=null) || (childrenQueue.get(i)!=null && childrenQueue.get(j)==null)){ return false; } if(childrenQueue.get(i).val == childrenQueue.get(j).val){ continue; } return false; } } childrenQueue.clear(); } } return true; } }
[leetcode]Symmetric Tree - java
猜你喜欢
转载自blue2048.iteye.com/blog/2196631
今日推荐
周排行