diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 05a67623c4..b7d4155746 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -254,93 +254,108 @@ public: ## 其他语言版本 Java: -```java -public class N0101 { + +```Java /** - * 解法1:DFS,递归。 + * 递归法 */ - public boolean isSymmetric2(TreeNode root) { - if (root == null) { - return false; - } - + public boolean isSymmetric1(TreeNode root) { return compare(root.left, root.right); } private boolean compare(TreeNode left, TreeNode right) { - if (left == null && right == null) { - return true; + + if (left == null && right != null) { + return false; } if (left != null && right == null) { return false; } - if (left == null && right != null) { + + if (left == null && right == null) { + return true; + } + if (left.val != right.val) { return false; } + // 比较外侧 + boolean compareOutside = compare(left.left, right.right); + // 比较内侧 + boolean compareInside = compare(left.right, right.left); + return compareOutside && compareInside; + } - if (left.val == right.val) { - return compare(left.left, right.right) && compare(left.right, right.left); + /** + * 迭代法 + * 使用双端队列,相当于两个栈 + */ + public boolean isSymmetric2(TreeNode root) { + Deque deque = new LinkedList<>(); + deque.offerFirst(root.left); + deque.offerLast(root.right); + while (!deque.isEmpty()) { + TreeNode leftNode = deque.pollFirst(); + TreeNode rightNode = deque.pollLast(); + if (leftNode == null && rightNode == null) { + continue; + } +// if (leftNode == null && rightNode != null) { +// return false; +// } +// if (leftNode != null && rightNode == null) { +// return false; +// } +// if (leftNode.val != rightNode.val) { +// return false; +// } + // 以上三个判断条件合并 + if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) { + return false; + } + deque.offerFirst(leftNode.left); + deque.offerFirst(leftNode.right); + deque.offerLast(rightNode.right); + deque.offerLast(rightNode.left); } - - return false; + return true; } /** - * 解法2:DFS,迭代 + * 迭代法 + * 使用普通队列 */ public boolean isSymmetric3(TreeNode root) { - if (root == null) { - return false; - } - - if (!equal(root.left, root.right)) { - return false; - } - - Deque st = new LinkedList<>(); - - st.push(root.right); - st.push(root.left); - - TreeNode curR = root.right; - TreeNode curL = root.left; - - while (!st.isEmpty()) { - curL = st.pop(); - curR = st.pop(); - - // 前序,处理 - if (!equal(curL, curR)) { - return false; + Queue deque = new LinkedList<>(); + deque.offer(root.left); + deque.offer(root.right); + while (!deque.isEmpty()) { + TreeNode leftNode = deque.poll(); + TreeNode rightNode = deque.poll(); + if (leftNode == null && rightNode == null) { + continue; } - - if (curR != null && curL != null) { - st.push(curL.right); - st.push(curR.left); - st.push(curR.right); - st.push(curL.left); +// if (leftNode == null && rightNode != null) { +// return false; +// } +// if (leftNode != null && rightNode == null) { +// return false; +// } +// if (leftNode.val != rightNode.val) { +// return false; +// } + // 以上三个判断条件合并 + if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) { + return false; } + // 这里顺序与使用Deque不同 + deque.offer(leftNode.left); + deque.offer(rightNode.right); + deque.offer(leftNode.right); + deque.offer(rightNode.left); } - return true; } - private boolean equal(TreeNode l, TreeNode r) { - if (l == null && r == null) { - return true; - } - if (l != null && r == null) { - return false; - } - if (l == null && r != null) { - return false; - } - if (l.val == r.val) { - return true; - } - return false; - } -} ``` Python: