-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathProblem_03.java
85 lines (79 loc) · 2.15 KB
/
Problem_03.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Cracking-The-Coding-Interview
* Problem_03.java
*/
package com.deepak.ctci.Ch04_Trees_And_Graphs;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import com.deepak.ctci.Library.TreeNode;
/**
* <br> Problem Statement :
*
* Given a binary tree, design an algorithm which
* creates a linked list of all the nodes at each depth.
* Ex. If you have a tree with depth D, you will have
* D linked lists.
*
* </br>
*
* @author Deepak
*/
public class Problem_03 {
/**
* Method to create a linked list at each depth
*
* @param root
* @return {@link List<LinkedList<TreeNode<T>>>}
*/
public static <T> List<LinkedList<TreeNode<T>>> createLinkedListAtEachDepth(TreeNode<T> root) {
/* If root is null, no list can be created */
if (root == null) {
return null;
}
/* Create a list to hold results */
List<LinkedList<TreeNode<T>>> result = new ArrayList<>();
/* Visit the root */
LinkedList<TreeNode<T>> current = new LinkedList<>();
current.add(root);
/* Keep going until elements are there */
while (current.size() > 0) {
/* Add current level and move to next level */
result.add(current);
LinkedList<TreeNode<T>> parents = current;
current = new LinkedList<>();
/* Add children for each parent node */
for (TreeNode<T> parent : parents) {
if (parent.left != null) {
current.add(parent.left);
}
if (parent.right != null) {
current.add(parent.right);
}
}
}
return result;
}
/**
* Method to print result
*
* @param result
*/
public static <T> void printResult(List<LinkedList<TreeNode<T>>> result) {
int depth = 0;
/* Loop through each entry is the list and get iterator */
for (LinkedList<TreeNode<T>> entry : result) {
Iterator<TreeNode<T>> iterator = entry.iterator();
System.out.println("Linked List at depth " + depth + " : ");
/* Keep printing until we have elements in the iterator */
while (iterator.hasNext()) {
/* Get the node and print its data */
TreeNode<T> treeNode = (TreeNode<T>) iterator.next();
System.out.println(treeNode.data);
}
System.out.println();
depth++;
}
}
}