Skip to content

Commit af8a7be

Browse files
committed
finish "101. Symmetric Tree". Fix #101
1 parent cc03268 commit af8a7be

File tree

6 files changed

+236
-6
lines changed

6 files changed

+236
-6
lines changed

README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,12 @@
508508
//|{leetcode_base_url}/same-tree/[Same Tree]
509509
//|{source_base_url}/SameTree.java[Java]
510510
//|Easy
511-
//
512-
//|101
513-
//|{leetcode_base_url}/symmetric-tree/[Symmetric Tree]
514-
//|{source_base_url}/SymmetricTree.java[Java]
515-
//|Easy
516-
//
511+
512+
|101
513+
|{leetcode_base_url}/symmetric-tree/[Symmetric Tree]
514+
|{source_base_url}/SymmetricTree.java[Java]
515+
|Easy
516+
517517
//|102
518518
//|{leetcode_base_url}/binary-tree-level-order-traversal/[Binary Tree Level Order Traversal]
519519
//|{source_base_url}/BinaryTreeLevelOrderTraversal.java[Java]

docs/0101-symmetric-tree.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
= 101. Symmetric Tree
2+
3+
为什么执行结果显示递归更快?而不是队列呢?

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2424
</properties>
25+
<dependencies>
26+
<dependency>
27+
<groupId>com.fasterxml.jackson.core</groupId>
28+
<artifactId>jackson-databind</artifactId>
29+
<version>2.10.1</version>
30+
</dependency>
31+
</dependencies>
2532

2633
<build>
2734
<plugins>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.diguage.algorithm.util;
2+
3+
/**
4+
* @author D瓜哥, https://www.diguage.com/
5+
* @since 2020-01-02 00:20
6+
*/
7+
public class TreeNode {
8+
public int val;
9+
public TreeNode left;
10+
public TreeNode right;
11+
12+
public TreeNode(int x) {
13+
val = x;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.diguage.algorithm.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.Objects;
10+
11+
/**
12+
* @author D瓜哥, https://www.diguage.com/
13+
* @since 2020-01-02 00:25
14+
*/
15+
public class TreeNodeUtils {
16+
public static TreeNode buildTree(List<Integer> array) {
17+
if (Objects.isNull(array) || array.size() == 0) {
18+
return null;
19+
}
20+
21+
TreeNode result = new TreeNode(array.get(0));
22+
List<TreeNode> treeNodeList = new ArrayList<>(array.size());
23+
treeNodeList.add(result);
24+
for (int i = 1; i < array.size(); i++) {
25+
TreeNode parent = treeNodeList.get((i - 1) / 2);
26+
if (Objects.isNull(parent)) {
27+
continue;
28+
}
29+
Integer value = array.get(i);
30+
TreeNode treeNode = null;
31+
if (Objects.nonNull(value)) {
32+
treeNode = new TreeNode(value);
33+
if (i % 2 == 0) {
34+
parent.right = treeNode;
35+
} else {
36+
parent.left = treeNode;
37+
}
38+
}
39+
treeNodeList.add(treeNode);
40+
}
41+
return result;
42+
}
43+
44+
public static void main(String[] args) throws JsonProcessingException {
45+
ObjectMapper mapper = new ObjectMapper();
46+
List<Integer> integers = Arrays.asList(1, 2, 2, 3, 4, 4, 3);
47+
TreeNode treeNode = buildTree(integers);
48+
System.out.println(mapper.writeValueAsString(treeNode));
49+
50+
List<Integer> num = Arrays.asList(1, 2, 2, null, 3, null, 3);
51+
TreeNode tree = buildTree(num);
52+
System.out.println(mapper.writeValueAsString(tree));
53+
}
54+
}

0 commit comments

Comments
 (0)