#coding exercise
Given a binary tree, check if its symmetric around the center
bool isSymmetric( Node root)
{
if (root == null) return false;
List<List<Node>> items= GetZigZag(root); // level wise items (already implemented earlier)
for (int i = 0; i < items.Count; i++)
if (IsPalindrome(items[i]) == false)
return false;
return true;
}
bool isPalindrome(List<Node> items)
{
if (items == null) return false;
int start = 0;
int end = items.Count - 1;
while ( start < end)
{
if (items[start] != items[end])
return false;
start++;
end--;
}
return true;
}
Given a pair of rectangles aligned along the axis in the positive quadrant and given by
class Rectangle
{
Point tl; // TopLeft;
Point br; // BottomRight;
}
class Point
{
int x;
int y;
}
Implement the following methods
static bool IsIntersect( Rectangle r1, Rectangle r2);
static Rectangle Intersecting(Rectangle r1, Rectangle r2);
static bool IsIntersect( Rectangle r1, Rectangle r2)
{
bool isdisjoint = false;
// are they disjoint along x axis ?
if (r1.tl.x < r2.tl.x)
isdisjoint = r1.br.x < r2.tl.x;
else
isdisjoint = r2.br.x < r1.tl.x;
if (isdisjoint == true) return false;
// are they disjoint along y-axis ?
if (r1.br.y < r2.br.y)
isdisjoint = r1.tl.y < r2.br.y;
else
isdisjoint = r2.tl.y < r1.br.y;
return isdisjoint == false;
}
static Rectangle Intersecting(Rectangle r1, Rectangle r2)
{
if (!IsIntersect(r1, r2)) return null;
Rectangle r = new Rectangle();
Rectangle left = (r1.tl.x <= r2.tl.x) ? r1 : r2;
Rectangle right = (r1.tl.x <= r2.tl.x) ? r2 : r1;
Rectangle bottom = (r1.tl.y <= r2.tl.y) ? r1 : r2;
Rectangle top = (r1.tl.y <= r2.tl.y) ? r1 : r2;
r.tl.x = right.tl.x;
r.br.x = right.br.x <= left.br.x ? right.br.x : left.br.x;
r.tl.y = bottom.tl.y;
r.br.y = bottom.br.y <= top.br.y ? top.br.y : bottom.br.y;
return r;
}
No comments:
Post a Comment