-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSolution.java
63 lines (51 loc) · 1.91 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @author Oleg Cherednik
* @since 18.02.2020
*/
public class Solution {
public static void main(String... args) {
Node root1 = new Node(4, 3);
root1.children[0] = new Node(3, 2);
root1.children[2] = new Node(5, 0);
root1.children[2] = new Node(3, 2);
root1.children[0].children[0] = new Node(9, 0);
root1.children[2].children[1] = new Node(9, 0);
Node root2 = new Node(4, 2);
root2.children[0] = new Node(3, 3);
root2.children[1] = new Node(3, 3);
root2.children[0].children[0] = new Node(9, 0);
root2.children[0].children[1] = new Node(4, 0);
root2.children[0].children[2] = new Node(1, 0);
root2.children[1].children[0] = new Node(1, 0);
root2.children[1].children[1] = new Node(4, 0);
root2.children[1].children[2] = new Node(9, 0);
System.out.println(isSymmetric(root1)); // true
System.out.println(isSymmetric(root2)); // true
}
private static final class Node {
private final int val;
private final Node[] children;
public Node(int val, int totalChildren) {
this.val = val;
children = new Node[totalChildren];
}
}
public static boolean isSymmetric(Node root) {
for (int i = 0, j = root.children.length - 1; i < j; i++, j--)
if (!isSymmetric(root.children[i], root.children[j]))
return false;
return true;
}
private static boolean isSymmetric(Node one, Node two) {
if (one == null ^ two == null)
return false;
if (one.val != two.val)
return false;
if (one.children.length != two.children.length)
return false;
for (int i = 0, j = one.children.length - 1; i < j; i++, j--)
if (!isSymmetric(one.children[i], two.children[j]))
return false;
return true;
}
}