-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlevelOrder.java
More file actions
46 lines (37 loc) · 778 Bytes
/
levelOrder.java
File metadata and controls
46 lines (37 loc) · 778 Bytes
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.io.*;
import java.util.*;
class Node{
int data;
Node left;
Node right;
Node(int key){
data = key;
left=right=null;
}
}
public class levelOrder{
public static void traversal(Node root){
if(root==null)
return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while(q.isEmpty()==false){
Node temp = q.poll();
System.out.print(temp.data+" ");
if(temp.left!=null)
q.add(temp.left);
if(temp.right!=null)
q.add(temp.right);
}
}
public static void main(String[] args) {
Node node = new Node(10);
node.left = new Node(30);
node.right = new Node(50);
node.left.left = new Node(70);
node.left.right = new Node(90);
node.right.left = new Node(110);
node.right.right = new Node(130);
traversal(node);
}
}