-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_01.java
More file actions
50 lines (38 loc) · 1021 Bytes
/
Copy pathBT_Problem_01.java
File metadata and controls
50 lines (38 loc) · 1021 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
47
48
49
50
package trees.binaryTree;
import java.util.*;
// Find Level order traversal of binary tree
class Node{
int data;
int hd;
Node left, right;
public Node(int item) {
data = item;
hd = Integer.MAX_VALUE;
left = right = null;
}
}
public class BT_Problem_01 {
Node root;
void printLevelOrder() {
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()) {
Node tempNode = queue.poll();
System.out.print(tempNode.data + " ");
if(tempNode.left != null)
queue.add(tempNode.left);
if(tempNode.right != null)
queue.add(tempNode.right);
}
}
public static void main(String[] args) {
BT_Problem_01 tree_level = new BT_Problem_01();
tree_level.root = new Node(1);
tree_level.root.left = new Node(2);
tree_level.root.right = new Node(3);
tree_level.root.left.left = new Node(4);
tree_level.root.left.right = new Node(5);
System.out.println("Level order traversal of binary tree is - ");
tree_level.printLevelOrder();
}
}