File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change 1- const Queue = require ( '../stacks-and-queues/queue' ) ;
21const LinkedList = require ( '../linked-list/linkedlist' ) ;
32
43function listOfDepths ( root ) {
4+ const result = [ ] ;
5+
6+ if ( ! root ) return result ;
7+
8+ let current = new LinkedList ( ) ;
9+
10+ current . addLast ( root ) ;
11+
12+ while ( current . size ( ) ) {
13+ result . push ( current ) ;
14+
15+ const parents = current ;
16+ current = new LinkedList ( ) ;
17+
18+ for ( let parent = parents . head ; parent ; parent = parent . next ) {
19+ parent . data . adjacents . forEach ( function ( adj ) {
20+ current . addLast ( adj ) ;
21+ } ) ;
22+ }
23+ }
24+
25+ return result ;
26+ }
27+
28+ // ------- Alternative solution
29+
30+ const Queue = require ( '../stacks-and-queues/queue' ) ;
31+
32+ function listOfDepths2 ( root ) {
533 const queue = new Queue ( ) ;
634 const list = [ ] ;
735 let depth = 0 ;
You can’t perform that action at this time.
0 commit comments