-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_15.java
More file actions
66 lines (51 loc) · 1.41 KB
/
Copy pathBT_Problem_15.java
File metadata and controls
66 lines (51 loc) · 1.41 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package trees.binaryTree;
import java.util.*;
import java.util.Map.Entry;
/*
* Problem Title :- Diagonal Traversal of a Binary tree
*/
public class BT_Problem_15 {
static class Node{
int data;
Node left, right;
Node(int data){
this.data = data;
left = null;
right = null;
}
}
static void diagonalPrintUtil(Node root, int d, HashMap<Integer, Vector<Integer>> diagonalPrint) {
if(root == null)
return;
Vector<Integer> k = diagonalPrint.get(d);
if(k == null) {
k = new Vector<>();
k.add(root.data);
}
else {
k.add(root.data);
}
diagonalPrint.put(d,k);
diagonalPrintUtil(root.left, d+1, diagonalPrint);
diagonalPrintUtil(root.right, d, diagonalPrint);
}
static void diagonalPrint(Node root) {
HashMap<Integer, Vector<Integer>> diagonalPrint = new HashMap<>();
diagonalPrintUtil(root, 0, diagonalPrint);
System.out.println("Diagonal Traversal of Binary Tree");
for(Entry<Integer, Vector<Integer>> entry : diagonalPrint.entrySet())
System.out.println(entry.getValue());
}
public static void main(String[] args) {
Node root = new Node(8);
root.left = new Node(3);
root.right = new Node(10);
root.left.left = new Node(1);
root.left.right = new Node(6);
root.right.right = new Node(14);
root.right.right.left = new Node(13);
root.left.right.left = new Node(4);
root.left.right.right = new Node(7);
diagonalPrint(root);
}
}