-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelOrderTree.java
46 lines (40 loc) · 981 Bytes
/
LevelOrderTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
class Node1{
int data;
Node1 left,right;
public Node1(int key){
data=key;
left=right=null;
}
}
public class LevelOrderTree {
Node1 root;
void LevelOrder()
{
Queue<Node1> q=new LinkedList<Node1>();
q.add(root);
while(!q.isEmpty())
{
Node1 tempnode=q.poll();
System.out.print(tempnode.data+" ");
if(tempnode.left!=null)
{
q.add(tempnode.left);
}
if(tempnode.right!=null)
{
q.add(tempnode.right);
}
}
}
public static void main(String ar[])
{
LevelOrderTree tree=new LevelOrderTree();
tree.root=new Node1(1);
tree.root.left=new Node1(2);
tree.root.right=new Node1(3);
tree.root.left.left=new Node1(4);
tree.root.left.right=new Node1(5);
tree.LevelOrder();
}
}