/** * 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; } }
相关推荐
java java_leetcode-101-symmetric-tree
Symmetric tree - Java Recursive - Java Iterative - C Recursive - Python Iterative 0102 Binary Tree Level Order Traversal - Python3 iterative 0103 Binary Tree Zigzag Level Order Traversal - ...
例如,树的层级遍历(Levelorder Traversal)、判断树的对称性(Symmetric Tree)、找到二叉搜索树中距离某个值最近的节点(Closest Binary Search Tree Value)等。这些题目通常要求编写者熟悉树的结构和遍历方法,...
- **Symmetric Tree**:判断一个二叉树是否是对称的。 - **Same Tree**:判断两棵二叉树是否相同。 - **Balanced Binary Tree**:判断一个二叉树是否是平衡的。 - **Path Sum**:判断是否存在一条路径,其节点值...
Leetcode 使用 Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js...