Hackerrank Day 23
Level order traversal of tree
static int getHeight(Node root)
{
if (root == null)
{
return -1;
}
int max_left = getHeight(root.left);
int max_right = getHeight(root.right);
if (max_left > max_right)
return max_left + 1;
else
return max_right + 1;
//Write your code here
}
static void levelOrder(Node root){
int height = getHeight(root);
for (int i = 1; i <= height+1; i++)
{
getlevelOrder(root, i);
}
//Write your code here
}
static void getlevelOrder(Node root, int d)
{
if (root == null)
return;
if (d == 1)
{
Console.Write(root.data);
}
else
{
getlevelOrder(root.left,d-1);
getlevelOrder(root.right,d-1);
}
}
static int getHeight(Node root)
{
if (root == null)
{
return -1;
}
int max_left = getHeight(root.left);
int max_right = getHeight(root.right);
if (max_left > max_right)
return max_left + 1;
else
return max_right + 1;
//Write your code here
}
static void levelOrder(Node root){
int height = getHeight(root);
for (int i = 1; i <= height+1; i++)
{
getlevelOrder(root, i);
}
//Write your code here
}
static void getlevelOrder(Node root, int d)
{
if (root == null)
return;
if (d == 1)
{
Console.Write(root.data);
}
else
{
getlevelOrder(root.left,d-1);
getlevelOrder(root.right,d-1);
}
}
Comments
Post a Comment