-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Is_symmetric_tree.java
154 lines (142 loc) · 4.01 KB
/
Is_symmetric_tree.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Is Symmetric Tree
For a given a Binary Tree,
your task is to check whether it is Symmetric or not,
i.e. whether that binary tree is a Mirror image of itself or not.
Like--
5
/ \
2 2
/ \
3 3
This is a symmetric tree.
*/
import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
import java.util.*;
//defining a class for storing nodes of tree
class Node
{
int value;
Node left;
Node right;
Node(int value)
{
this.value = value;
left=null;
right=null;
}
}
class Checksymmetric
{
// function to return true/false accordingly if the tree is Symmetric or if not
public static boolean isSymmetric(Node root)
{
// If the tree is empty then obviously it is symmtric
if(root == null)
{
return true;
}
// Otherwise
return util(root.left, root.right);
}
public static boolean util(Node n1,Node n2)
{
// If the trees are empty then they are symmetric
if(n1 == null && n2 == null)
{
return true;
}
// If any one is empty then obviously is not equal to that tree
// and hence not symmetric
if(n1 == null || n2 == null)
{
return false;
}
// Otherwise
return n1.value == n2.value && util(n1.left, n2.right) && util(n1.right, n2.left);
}
}
//driver code
class Is_symmetric_tree
{
// Function to build the tree
static Node buildTree(String str)
{
if(str.length() == 0 || str.charAt(0) == 'N')
{
return null;
}
String ip[] = str.split(" ");
// Here we start creating the root of the tree
Node root = new Node(Integer.parseInt(ip[0]));
// Pushing the roots to the queue
Queue<Node> Treequeue = new LinkedList<>();
Treequeue.add(root);
// Starting from the second element
int i = 1;
while(Treequeue.size() > 0 && i < ip.length)
{
// Fetching and removing front of the queue
Node currNode = Treequeue.peek();
Treequeue.remove();
// Getting current node value from the string
String currVal = ip[i];
// If the left child is not null then
if(!currVal.equals("N"))
{
// Creating left child for the current node
currNode.left = new Node(Integer.parseInt(currVal));
// And then Pushing it to into the queue
Treequeue.add(currNode.left);
}
i++;
// For the right child
if(i >= ip.length)
{
break;
}
currVal = ip[i];
// If the right child is not null then
if(!currVal.equals("N"))
{
// Creating right child for the current node
currNode.right = new Node(Integer.parseInt(currVal));
// And then Pushing it to into the queue
Treequeue.add(currNode.right);
}
i++;
}
return root;
}
public static void main (String[] args) throws IOException
{
// Taking input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the values of nodes for tree : ");
String str = br.readLine();
Node root = buildTree(str);
// For output
Checksymmetric g = new Checksymmetric();
System.out.println("Check for a symmetric tree gives : ");
if(g.isSymmetric(root) == true)
{
System.out.print("Symmetric");
}
else
{
System.out.print("Not Symmetric");
}
System.out.println(" ");
}
}
/*
EXAMPLE:-
Input--
Enter the values of nodes for tree : 1 2 2 3 4 4 3
Output--
Check for a symmetric tree gives : Symmetric
TIME COMPLEXITY --> O(N)
SPACE COMPLEXITY --> O(N) ; where N is the total number of nodes in the tree
*/