Symmetric Tree or Mirror Tree
unknown
java
8 months ago
2.6 kB
8
Indexable
import java.util.*;
// Node structure for the binary tree
class Node {
int data;
Node left;
Node right;
// Constructor to initialize
// the node with a value
public Node(int val) {
data = val;
left = null;
right = null;
}
}
class Solution {
// Function to check if
// two subtrees are symmetric
private boolean isSymmetricUtil(Node root1, Node root2) {
// Check if either subtree is null
if (root1 == null || root2 == null) {
// If one subtree is null, the other
// must also be null for symmetry
return root1 == root2;
}
// Check if the data in the current nodes is equal
// and recursively check for symmetry in subtrees
return (root1.data == root2.data)
&& isSymmetricUtil(root1.left, root2.right)
&& isSymmetricUtil(root1.right, root2.left);
}
// Public function to check if the
// entire binary tree is symmetric
public boolean isSymmetric(Node root) {
// Check if the tree is empty
if (root == null) {
// An empty tree is
// considered symmetric
return true;
}
// Call the utility function
// to check symmetry of subtrees
return isSymmetricUtil(root.left, root.right);
}
}
public class Main {
// Function to print the Inorder
// Traversal of the Binary Tree
private static void printInorder(Node root) {
if (root == null) {
return;
}
printInorder(root.left);
System.out.print(root.data + " ");
printInorder(root.right);
}
public static void main(String[] args) {
// Creating a sample binary tree
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(2);
root.left.left = new Node(3);
root.right.right = new Node(3);
root.left.right = new Node(4);
root.right.left = new Node(4);
Solution solution = new Solution();
System.out.print("Binary Tree (Inorder): ");
printInorder(root);
System.out.println();
boolean res = solution.isSymmetric(root);
if (res) {
System.out.println("This Tree is Symmetrical");
} else {
System.out.println("This Tree is NOT Symmetrical");
}
}
}
Editor is loading...
Leave a Comment