File tree Expand file tree Collapse file tree 2 files changed +3
-40
lines changed
src/hackerrank/interview_preparation_kit/search Expand file tree Collapse file tree 2 files changed +3
-40
lines changed Original file line number Diff line number Diff line change @@ -115,15 +115,6 @@ export function flatTree(root) {
115115 return output ;
116116}
117117
118- export function swapBranch ( root ) {
119- if ( root ) {
120- // eslint-disable-next-line no-param-reassign
121- [ root . left , root . right ] = [ root . right , root . left ] ;
122- }
123-
124- return root ;
125- }
126-
127118export function swapNodes ( indexes , queries ) {
128119 const tree = buildTree ( indexes ) ;
129120 const output = [ ] ;
@@ -152,7 +143,8 @@ export function swapNodes(indexes, queries) {
152143
153144 if ( tLevel % query === 0 ) {
154145 for ( const node of nodeList ) {
155- swapBranch ( node ) ;
146+ // swap branches
147+ [ node . left , node . right ] = [ node . right , node . left ] ;
156148 }
157149 }
158150 }
Original file line number Diff line number Diff line change 11import { describe , expect , it } from '@jest/globals' ;
22
3- import { Node } from '../../lib/Node.js' ;
4- import {
5- buildTree ,
6- flatTree ,
7- swapBranch ,
8- swapNodes ,
9- __INITIAL_LEVEL__
10- } from './swap_nodes_algo.js' ;
3+ import { buildTree , flatTree , swapNodes } from './swap_nodes_algo.js' ;
114import TESTCASES from './swap_nodes_algo.testcases.json' ;
125
136describe ( 'swap_nodes_algo' , ( ) => {
@@ -33,28 +26,6 @@ describe('swap_nodes_algo', () => {
3326 expect ( tresult ) . toStrictEqual ( expected ) ;
3427 } ) ;
3528
36- it ( 'testswapBranch empty' , ( ) => {
37- expect . assertions ( 1 ) ;
38-
39- const tInput = null ;
40- const tresult = swapBranch ( tInput ) ;
41- const expected = null ;
42-
43- expect ( tresult ) . toStrictEqual ( expected ) ;
44- } ) ;
45-
46- it ( 'testswapBranch' , ( ) => {
47- expect . assertions ( 1 ) ;
48-
49- const tInput = new Node ( __INITIAL_LEVEL__ ) ;
50- tInput . left = new Node ( 2 ) ;
51- tInput . right = new Node ( 3 ) ;
52- const tresult = flatTree ( swapBranch ( tInput ) ) ;
53- const expected = [ 3 , 1 , 2 ] ;
54-
55- expect ( tresult ) . toStrictEqual ( expected ) ;
56- } ) ;
57-
5829 it ( 'build tree and flattened tree test cases' , ( ) => {
5930 expect . assertions ( 4 ) ;
6031
You can’t perform that action at this time.
0 commit comments