@@ -22,6 +22,7 @@ public class ListOfDepths {
22
22
private static List <List <TreeNode >> listOfDepths (TreeNode root ) {
23
23
List <List <TreeNode >> listOfDepths = new ArrayList <>();
24
24
List <TreeNode > listOfNodesAtCurrentDepth = new ArrayList <>();
25
+
25
26
if (root != null ) {
26
27
listOfNodesAtCurrentDepth .add (root );
27
28
}
@@ -31,7 +32,7 @@ private static List<List<TreeNode>> listOfDepths(TreeNode root) {
31
32
List <TreeNode > listOfNodesAtPreviousDepth = listOfNodesAtCurrentDepth ; // make current depth as previous
32
33
/* make current depth as the new depth to be processed considering
33
34
the nodes from the previous depth as parents */
34
- listOfNodesAtCurrentDepth = new ArrayList <>();
35
+ listOfNodesAtCurrentDepth = new ArrayList <>();
35
36
36
37
for (TreeNode node : listOfNodesAtPreviousDepth ) {
37
38
if (node .left != null ) {
@@ -46,6 +47,42 @@ private static List<List<TreeNode>> listOfDepths(TreeNode root) {
46
47
return listOfDepths ;
47
48
}
48
49
50
+ /**
51
+ * This is a recursive approach where we pass the depth of each node in the call. We use a
52
+ * list {@code listOfDepths} to keep track of all the depths.
53
+ *
54
+ * @param node
55
+ * @param depth
56
+ * @param listOfDepths
57
+ * @return list of nodes at each depth, where depth starts from 0
58
+ */
59
+ private static List <List <TreeNode >> listOfDepths (TreeNode node , int depth , List <List <TreeNode >> listOfDepths ) {
60
+ if (node == null ) return null ;
61
+
62
+ List <TreeNode > listOfNodesAtDepth ;
63
+ if (depth == listOfDepths .size ()) {
64
+ listOfNodesAtDepth = new ArrayList <>();
65
+ listOfDepths .add (listOfNodesAtDepth );
66
+ } else {
67
+ listOfNodesAtDepth = listOfDepths .get (depth );
68
+ }
69
+
70
+ listOfNodesAtDepth .add (node );
71
+
72
+ listOfDepths (node .left , depth + 1 , listOfDepths );
73
+ listOfDepths (node .right , depth + 1 , listOfDepths );
74
+
75
+ return listOfDepths ;
76
+ }
77
+
78
+ private static void printAllDepths (List <List <TreeNode >> listOfDepths ) {
79
+ for (int i = 0 ; i < listOfDepths .size (); i ++) {
80
+ System .out .print ("Depth " + i + ": " );
81
+ listOfDepths .get (i ).forEach (node -> System .out .print ("->" + node .val ));
82
+ System .out .println ();
83
+ }
84
+ }
85
+
49
86
public static void main (String [] args ) {
50
87
TreeNode treeRoot = new TreeNode (1 );
51
88
treeRoot .left = new TreeNode (2 );
@@ -55,11 +92,8 @@ public static void main(String[] args) {
55
92
treeRoot .right .left = new TreeNode (6 );
56
93
treeRoot .right .right = new TreeNode (7 );
57
94
58
- List <List <TreeNode >> listOfDepths = listOfDepths (treeRoot );
59
- for (int i = 0 ; i < listOfDepths .size (); i ++) {
60
- System .out .print ("Depth " + i + ": " );
61
- listOfDepths .get (i ).forEach (node -> System .out .print ("->" + node .val ));
62
- System .out .println ();
63
- }
95
+ printAllDepths (listOfDepths (treeRoot ));
96
+ System .out .println ("-----" );
97
+ printAllDepths (listOfDepths (treeRoot , 0 , new ArrayList <>()));
64
98
}
65
99
}
0 commit comments