using
System;
using
System.Collections.Generic;
class
Node
{
public
int
Data;
public
Node Left, Right;
public
Node(
int
data)
{
Data = data;
Left = Right =
null
;
}
}
class
Solution
{
public
bool
IsMirrorIterative(Node root1, Node root2)
{
if
(root1 ==
null
&& root2 ==
null
)
return
true
;
if
(root1 ==
null
|| root2 ==
null
)
return
false
;
Stack<Node> stack1 =
new
Stack<Node>();
Stack<Node> stack2 =
new
Stack<Node>();
stack1.Push(root1);
stack2.Push(root2);
while
(stack1.Count > 0 && stack2.Count > 0)
{
Node curr1 = stack1.Pop();
Node curr2 = stack2.Pop();
if
(curr1.Data != curr2.Data)
return
false
;
if
(curr1.Left !=
null
&& curr2.Right !=
null
)
{
stack1.Push(curr1.Left);
stack2.Push(curr2.Right);
}
else
if
(curr1.Left !=
null
|| curr2.Right !=
null
)
return
false
;
if
(curr1.Right !=
null
&& curr2.Left !=
null
)
{
stack1.Push(curr1.Right);
stack2.Push(curr2.Left);
}
else
if
(curr1.Right !=
null
|| curr2.Left !=
null
)
return
false
;
}
if
(stack1.Count > 0 || stack2.Count > 0)
return
false
;
return
true
;
}
}
class
Program
{
static
void
Main()
{
Node root1 =
new
Node(1);
root1.Left =
new
Node(2);
root1.Right =
new
Node(3);
root1.Left.Left =
new
Node(4);
root1.Left.Right =
new
Node(5);
Node root2 =
new
Node(1);
root2.Left =
new
Node(3);
root2.Right =
new
Node(2);
root2.Right.Left =
new
Node(5);
root2.Right.Right =
new
Node(4);
Solution solution =
new
Solution();
if
(solution.IsMirrorIterative(root1, root2))
Console.WriteLine(
"Yes"
);
else
Console.WriteLine(
"No"
);
}
}