Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level Order Traversal of a Binary Tree in Java #557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions BTreeLevelOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
class TreeNode
{
int val;

TreeNode left, right;

public TreeNode(int i)
{
val = i;
right = left = null;
}
}

public class BTreeLevelOrder
{

TreeNode r;

public BTreeLevelOrder() { r = null; }

void displayLevelOrder()
{
int ht = treeHeight(r);
int j;

for (j = 1; j <= ht; j++)
{
displayCurrentLevel(r, j);
}
}

int treeHeight(TreeNode r)
{
if (r == null)
{
return 0;
}
else
{

int lh = treeHeight(r.left);
int rh = treeHeight(r.right);

if (lh > rh)
{
return (lh + 1);
}
else
{
return (rh + 1);
}
}
}

void displayCurrentLevel(TreeNode r, int l)
{

if (r == null)
{
return;
}

if (l == 1)
{
System.out.print(r.val + " ");
}

else if (l > 1)
{
displayCurrentLevel(r.left, l - 1);
displayCurrentLevel(r.right, l - 1);
}
}

public static void main(String argvs[])
{

BTreeLevelOrder tree = new BTreeLevelOrder ();

tree.r = new TreeNode(18);

tree.r.left = new TreeNode(20);
tree.r.right = new TreeNode(30);
tree.r.left.left = new TreeNode(60);
tree.r.left.right = new TreeNode(34);
tree.r.right.left = new TreeNode(45);
tree.r.right.right = new TreeNode(65);
tree.r.left.left.left = new TreeNode(12);
tree.r.left.left.right = new TreeNode(50);
tree.r.left.right.left = new TreeNode(98);
tree.r.left.right.right = new TreeNode(82);
tree.r.right.left.left = new TreeNode(31);
tree.r.right.left.right = new TreeNode(59);
tree.r.right.right.left = new TreeNode(71);
tree.r.right.right.right = new TreeNode(41);


System.out.println("Level order traversal of binary tree is ");
tree.displayLevelOrder();
}
}