Skip to content

Commit 3542f1c

Browse files
kumanoitakumar-indeedsiriak
authored
Add check if tree is symmetric or not (fixes TheAlgorithms#3471) (TheAlgorithms#3472)
Co-authored-by: Amit Kumar <[email protected]> Co-authored-by: Andrii Siriak <[email protected]>
1 parent 23eec39 commit 3542f1c

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
4+
5+
/**
6+
* Check if a binary tree is symmetric or not.
7+
* A binary tree is a symmetric tree if the left and right subtree of root are mirror image.
8+
* Below is a symmetric tree
9+
* 1
10+
* / \
11+
* 2 2
12+
* / \ / \
13+
* 3 4 4 3
14+
*
15+
* Below is not symmetric because values is different in last level
16+
* 1
17+
* / \
18+
* 2 2
19+
* / \ / \
20+
* 3 5 4 3
21+
* <p>
22+
* Approach:
23+
* Recursively check for left and right subtree of root
24+
* 1. left subtrees root's values should be equal right subtree's root value
25+
* 2. recursively check with left subtrees' left child VS right subtree's right child AND
26+
* left subtree's right child VS right subtree left child
27+
* Complexity
28+
* 1. Time: O(n)
29+
* 2. Space: O(lg(n)) for height of tree
30+
*
31+
* @author kumanoit on 10/10/22 IST 12:52 AM
32+
*/
33+
public class CheckTreeIsSymmetric {
34+
35+
public static boolean isSymmetric(Node root) {
36+
if (root == null) {
37+
return true;
38+
}
39+
return isSymmetric(root.left, root.right);
40+
}
41+
42+
private static boolean isSymmetric(Node leftSubtreeRoot, Node rightSubtreRoot) {
43+
if (leftSubtreeRoot == null && rightSubtreRoot == null) {
44+
return true;
45+
}
46+
47+
if (leftSubtreeRoot == null || rightSubtreRoot == null || leftSubtreeRoot.data != rightSubtreRoot.data) {
48+
return false;
49+
}
50+
51+
return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
/**
9+
* @author kumanoit on 10/10/22 IST 1:02 AM
10+
*/
11+
public class CheckTreeIsSymmetricTest {
12+
13+
@Test
14+
public void testRootNull() {
15+
assertTrue(CheckTreeIsSymmetric.isSymmetric(null));
16+
}
17+
18+
@Test
19+
public void testSingleNodeTree() {
20+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{100});
21+
assertTrue(CheckTreeIsSymmetric.isSymmetric(root));
22+
}
23+
24+
@Test
25+
public void testSymmetricTree() {
26+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1,2,2,3,4,4,3});
27+
assertTrue(CheckTreeIsSymmetric.isSymmetric(root));
28+
}
29+
30+
@Test
31+
public void testNonSymmetricTree() {
32+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1,2,2,3,5,4,3});
33+
assertFalse(CheckTreeIsSymmetric.isSymmetric(root));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)