-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP47.java
More file actions
42 lines (35 loc) · 1.16 KB
/
P47.java
File metadata and controls
42 lines (35 loc) · 1.16 KB
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
package binarytree;
public final class P47 {
private P47() {
}
/**
* 4.03 (**) Symmetric binary trees
* <p>
* https://www.baeldung.com/cs/binary-tree-is-symmetric
* Symmetric definition:
* * The two root nodes have the same value
* * The left subtree of one root node is a mirror reflection
* of the right subtree of the other root node
*/
public static boolean isSymmetricTree(AVLTree root) {
if (root.isEmpty() || root.root.height == 1) {
return true;
}
return isMirror(root.root.left, root.root.right);
}
private static boolean isMirror(AVLTree.BVSNode root1, AVLTree.BVSNode root2) {
if (root1 == null || root2 == null) {
return false;
}
if (root1.height == 1 && root2.height == 1) {
return true;
} else if (root1.height == 1 || root2.height == 1) {
return false;
} else {
// If you interest if content is also symmetric
// root1.data == root2.data &&
return isMirror(root1.left, root2.right) &&
isMirror(root1.right, root2.left);
}
}
}