-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonal Traversal
More file actions
42 lines (36 loc) · 1018 Bytes
/
Copy pathDiagonal Traversal
File metadata and controls
42 lines (36 loc) · 1018 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
Input : root = [8, 3, 10, 1, 6, N, 14, N, N, 4, 7, 13]
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Output : [8, 10, 14, 3, 6, 7, 13, 1, 4]
Explanation:
unnamed
Diagonal Traversal of binary tree : 8 10 14 3 6 7 13 1 4
class Tree
{
public ArrayList<Integer> diagonal(Node root)
{
//add your code here.
ArrayList<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
Queue<Node> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
Node curr = q.remove();
while(curr!=null){
result.add(curr.data);
if(curr.left!=null){
q.add(curr.left);
}
curr = curr.right;
}
}
return result;
}
}