Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JavaScript binary tree

//print Kth path (given K integer and you need to find the path to reach this K as total sum  in binary tree)
  const printKthPath =(root ) =>{
 let k =7 ;
if(root === null) return 
 let sum = 0 ; 
 let stack =[] ;
 sum =sum +root.data ;
 stack.push(root.data)
 if(sum === k){
  return stack
 }else {

   if(root.left)printKthPath(root.left )
  if(root.right)printKthPath(root.right)
 if(!root.right && ! root.left ){
   sum = sum- root.data;
    stack.pop();
 
 }
 }
return stack
  }
Source by github.com #
 
PREVIOUS NEXT
Tagged: #JavaScript #binary #tree
ADD COMMENT
Topic
Name
4+4 =