#codingexercise
Print all nodes at a distance k from a given node
void printKDistanceDown(Node root, int k)
{
if (root == null || k < 0) return;
if (k == 0)
Console.Write(root.data);
printKDistanceDown(root.left, k-1);
printKDistanceDown(root.right, k-1);
}
// returns distance of root from target
int PrintKDistanceNode(Node root, Node target, int k)
{
if (root == null) return -1;
if (root == target){printKDistanceDown(root, k); return 0;}
int dl = printKDistanceNode(root.left, target, k);
if (dl != -1)
{
if (dl + 1 == k)
Console.Write(root.data);
else
printKDistanceDown(root.right, k -dl-2);
return 1 + dl;
}
int dr = printKDistanceDown(root.right, target, k);
if (dr != -1)
{
if (dr +1 ==k)
Console.Write(root.data);
else
printKDistanceDown(root.left, k-dr-2);
return 1+dr;
}
return -1;
}
2. Given a string, recursively remove adjacent duplicate characters from string. The output string should not have any duplicates.
For eg: abcdaadhhhhhzppzl
becomes abchl
static String RemoveDuplicate(StringBuilder str, int n)
{
for (int m = 0; m < int.MaxValue; m++)
{
bool modified = false;
int k = 0;
int len = str.ToString().Length;
if (len == 0) return str.ToString();
for (int i = 0; i < len; i++)
{
if (k == 0) {
str[k] = str[i];
k++;
continue;
}
if (str[k] == '\0' || str[i] != str[k-1])
{
str[k] = str[i];
k++;
}
else
{
k--;
str[k] = '\0';
modified = true;
}
}
if (!modified)
break;
str[k] = '\0';
k++;
for (int i = k; i < len; i++)
str[i] = '\0';
Console.WriteLine("val =" + str.ToString());
}
return str.ToString();
}
Print all nodes at a distance k from a given node
void printKDistanceDown(Node root, int k)
{
if (root == null || k < 0) return;
if (k == 0)
Console.Write(root.data);
printKDistanceDown(root.left, k-1);
printKDistanceDown(root.right, k-1);
}
// returns distance of root from target
int PrintKDistanceNode(Node root, Node target, int k)
{
if (root == null) return -1;
if (root == target){printKDistanceDown(root, k); return 0;}
int dl = printKDistanceNode(root.left, target, k);
if (dl != -1)
{
if (dl + 1 == k)
Console.Write(root.data);
else
printKDistanceDown(root.right, k -dl-2);
return 1 + dl;
}
int dr = printKDistanceDown(root.right, target, k);
if (dr != -1)
{
if (dr +1 ==k)
Console.Write(root.data);
else
printKDistanceDown(root.left, k-dr-2);
return 1+dr;
}
return -1;
}
2. Given a string, recursively remove adjacent duplicate characters from string. The output string should not have any duplicates.
For eg: abcdaadhhhhhzppzl
becomes abchl
static String RemoveDuplicate(StringBuilder str, int n)
{
for (int m = 0; m < int.MaxValue; m++)
{
bool modified = false;
int k = 0;
int len = str.ToString().Length;
if (len == 0) return str.ToString();
for (int i = 0; i < len; i++)
{
if (k == 0) {
str[k] = str[i];
k++;
continue;
}
if (str[k] == '\0' || str[i] != str[k-1])
{
str[k] = str[i];
k++;
}
else
{
k--;
str[k] = '\0';
modified = true;
}
}
if (!modified)
break;
str[k] = '\0';
k++;
for (int i = k; i < len; i++)
str[i] = '\0';
Console.WriteLine("val =" + str.ToString());
}
return str.ToString();
}
No comments:
Post a Comment