/** * 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-tag-Tree
力扣解题java版本
leetcode 答案解析 golang解答
https://github.com/geekxingyun/leetcode-java-algorithm-solution.git 然后你会发现当前项目有两个模块。 完成:它是完成的项目,并有一些关于 leetcode 的答案。 初始:只需在模块项目中编写代码 如果您不知道...
用Java实现基础数据结构,排序算法、经典算法以及leetcode刷题记录_Java_下载.zip
java lru leetcode leetcode-algorithms-java leetcode 算法笔记-java
java lru leetcode LeetCode-Tag-Java 解决方案 LeetCode 的解决方案 指数
leetcode-helper-1.7.1
leetcode Leetcode-Java-解决方案 Java 中 Leetcode 问题的解决方案
LeetCode-Python-Java zhouzhenping/LeetCode-Python&Java 参考代码来源 力扣(LeetCode) 链接: 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 Github 01 两数之和 给定一个整数数组 ...
#Leetcode-In-Java 代码并不全是本人写的,有的参考了网络上其他前辈的想法,但都能在OJ上AC。 ###索引 1 . Two-Sum 要点: - 利用java中Array对象的sort方法排序,使得整个数组呈升序状态 - 再利用两段取点...
leetcode-tag-dynamic programming
分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, return all...
leetcode 答案LeetCode-答案-Java 我的 leetcode 用伪代码和分析用 java 回答
leetcode-tag-Stack
leetcode-tag-array
然后通过用循环来解:假设第一个for循环是一个数组的循环,而后它的内嵌循环是也是这个数组,只是下标从0变成了1,这样,在第一次循环时,第1个元素会与其他所有元素
LeetCode-Practice-Java leetcode 练习题 这个项目就是自己练习leetCode的实际Demo 有些公司就会问一些算法问题,即便是不问的话自己不学这块感觉也对不起自己的职业。。。 所以自己在学习的时候 也打算把优秀的答案...
LeetCode题解 - Java语言实现-181页.pdf
leetcode-for-Java Record the process of leetcode for Java. 引言 记录一下,在leetcode上刷题的过程。 前期以刷题为主。有空的话就更新一下这个目录,然后顺带把有写问题的解题思路写上。 目录