-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMirrorCheckTreesIterative
More file actions
94 lines (79 loc) · 2.38 KB
/
MirrorCheckTreesIterative
File metadata and controls
94 lines (79 loc) · 2.38 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
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
// Java program to see if two trees //In-order Traversal
// are mirror of each other //Time Complexity: O(n)
import java.util.*;
// A binary tree node
class Node
{
int data;
Node left, right;
public Node(int data)
{
this.data = data;
left = right = null;
}
}
class BinaryTree
{
Node a, b;
/* Given two trees, return true if they are
mirror of each other */
boolean areMirror(Node a, Node b)
{
Stack<Node> st1 = new Stack<Node>();
Stack<Node> st2 = new Stack<Node>();
while (true)
{
// iterative inorder traversal of 1st tree and
// reverse inoder traversal of 2nd tree
while (a!=null && b!=null)
{
// if the corresponding nodes in the two traversal
// have different data values, then they are not
// mirrors of each other.
if (a.data != b.data)
return false;
st1.push(a);
st2.push(b);
a = a.left;
b = b.right;
}
if (!(a == null && b == null))
return false;
if (!st1.isEmpty() && !st2.isEmpty())
{
a = st1.pop();
b = st2.pop();
/* we have visited the node and its left subtree.
Now, it's right subtree's turn */
a = a.right;
/* we have visited the node and its right subtree.
Now, it's left subtree's turn */
b = b.left;
}
// both the trees have been completely traversed
else
break;
}
// tress are mirrors of each other
return true;
}
// Driver code to test above methods
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
Node a = new Node(1);
Node b = new Node(1);
a.left = new Node(2);
a.right = new Node(3);
a.left.left = new Node(4);
a.left.right = new Node(5);
b.left = new Node(3);
b.right = new Node(2);
b.right.left = new Node(5);
b.right.right = new Node(4);
if (tree.areMirror(a, b) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}