|
| 1 | +package com.diguage.algorithm.leetcode; |
| 2 | + |
| 3 | +import com.diguage.algorithm.util.TreeNode; |
| 4 | +import com.diguage.algorithm.util.TreeNodeUtils; |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +/** |
| 9 | + * = 101. Symmetric Tree |
| 10 | + * |
| 11 | + * https://leetcode.com/problems/symmetric-tree/[Symmetric Tree - LeetCode] |
| 12 | + * |
| 13 | + * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). |
| 14 | + * |
| 15 | + * For example, this binary tree `[1,2,2,3,4,4,3]` is symmetric: |
| 16 | + * |
| 17 | + * ---- |
| 18 | + * 1 |
| 19 | + * / \ |
| 20 | + * 2 2 |
| 21 | + * / \ / \ |
| 22 | + * 3 4 4 3 |
| 23 | + * ---- |
| 24 | + * |
| 25 | + * But the following `[1,2,2,null,3,null,3]` is not: |
| 26 | + * |
| 27 | + * ---- |
| 28 | + * 1 |
| 29 | + * / \ |
| 30 | + * 2 2 |
| 31 | + * \ \ |
| 32 | + * 3 3 |
| 33 | + * ---- |
| 34 | + * |
| 35 | + * **Note:** |
| 36 | + * |
| 37 | + * Bonus points if you could solve it both recursively and iteratively. |
| 38 | + * |
| 39 | + * @author D瓜哥, https://www.diguage.com/ |
| 40 | + * @since 2020-01-02 00:20 |
| 41 | + */ |
| 42 | +public class SymmetricTree { |
| 43 | + public boolean isSymmetric(TreeNode root) { |
| 44 | + return isMirror(root, root); |
| 45 | + } |
| 46 | + |
| 47 | + private boolean isMirror(TreeNode t1, TreeNode t2) { |
| 48 | + if (Objects.isNull(t1) && Objects.isNull(t2)) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + if (Objects.isNull(t1)||Objects.isNull(t2)) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + return (Objects.equals(t1.val, t2.val)) |
| 55 | + && isMirror(t1.left, t2.right) |
| 56 | + && isMirror(t1.right, t2.left); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Runtime: 1 ms, faster than 36.88% of Java online submissions for Symmetric Tree. |
| 61 | + * |
| 62 | + * Memory Usage: 38.9 MB, less than 43.54% of Java online submissions for Symmetric Tree. |
| 63 | + */ |
| 64 | + public boolean isSymmetricIterative(TreeNode root) { |
| 65 | + Queue<TreeNode> queue = new LinkedList<>(); |
| 66 | + queue.add(root); |
| 67 | + queue.add(root); |
| 68 | + while (!queue.isEmpty()) { |
| 69 | + TreeNode t1 = queue.poll(); |
| 70 | + TreeNode t2 = queue.poll(); |
| 71 | + if (Objects.isNull(t1) && Objects.isNull(t2)) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + if (Objects.isNull(t1) || Objects.isNull(t2)) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + if (!Objects.equals(t1.val, t2.val)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + queue.add(t1.left); |
| 81 | + queue.add(t2.right); |
| 82 | + queue.add(t1.right); |
| 83 | + queue.add(t2.left); |
| 84 | + } |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Runtime: 228 ms, faster than 36.88% of Java online submissions for Symmetric Tree. |
| 90 | + * |
| 91 | + * Memory Usage: 92.6 MB, less than 5.44% of Java online submissions for Symmetric Tree. |
| 92 | + */ |
| 93 | + public boolean isSymmetricBfs(TreeNode root) { |
| 94 | + if (Objects.isNull(root)) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + ArrayList<TreeNode> parent = new ArrayList<>(); |
| 98 | + parent.add(root); |
| 99 | + while (!parent.isEmpty()) { |
| 100 | + HashSet<TreeNode> nodes = new HashSet<>(parent); |
| 101 | + if (nodes.size() == 1 && nodes.contains(null)) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + ArrayList<TreeNode> children = new ArrayList<>(parent.size() * 2); |
| 105 | + for (TreeNode node : parent) { |
| 106 | + if (Objects.isNull(node)) { |
| 107 | + children.add(null); |
| 108 | + children.add(null); |
| 109 | + } else { |
| 110 | + children.add(node.left); |
| 111 | + children.add(node.right); |
| 112 | + } |
| 113 | + } |
| 114 | + for (int i = 0; i < children.size() / 2; i++) { |
| 115 | + TreeNode left = children.get(i); |
| 116 | + TreeNode right = children.get(children.size() - 1 - i); |
| 117 | + if (Objects.isNull(left) && Objects.isNull(right)) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + if (Objects.isNull(left) || Objects.isNull(right)) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + if (!Objects.equals(left.val, right.val)) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + parent = children; |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + public static void main(String[] args) { |
| 134 | + SymmetricTree solution = new SymmetricTree(); |
| 135 | + |
| 136 | + List<Integer> a1 = Arrays.asList(1, 2, 2, 3, 4, 4, 3); |
| 137 | + TreeNode t1 = TreeNodeUtils.buildTree(a1); |
| 138 | + boolean r1 = solution.isSymmetric(t1); |
| 139 | + System.out.println((r1 == true) + " : " + r1); |
| 140 | + |
| 141 | + List<Integer> a2 = Arrays.asList(1, 2, 2, null, 3, null, 3); |
| 142 | + TreeNode t2 = TreeNodeUtils.buildTree(a2); |
| 143 | + boolean r2 = solution.isSymmetric(t2); |
| 144 | + System.out.println((r2 == false) + " : " + r2); |
| 145 | + |
| 146 | + List<Integer> a3 = Arrays.asList(1, 2, 2, null, 3, 3); |
| 147 | + TreeNode t3 = TreeNodeUtils.buildTree(a3); |
| 148 | + boolean r3 = solution.isSymmetric(t3); |
| 149 | + System.out.println((r3 == true) + " : " + r3); |
| 150 | + } |
| 151 | +} |
0 commit comments