SHARE
TWEET

Untitled

a guest Aug 20th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Queue = function() {
  2.     this.array = [];
  3.     this.count = 0;
  4. }
  5.  
  6. Queue.prototype.enqueue = function(val){
  7.     this.array.push(val);
  8.     this.count++;
  9. }
  10.  
  11. Queue.prototype.dequeue=function(){
  12.     var temp = this.array.shift();
  13.     this.count--;
  14.     return temp;
  15. }
  16.  
  17. Queue.prototype.size = function() {
  18.     return this.count;
  19. }
  20.  
  21. var Tree = function (val) {
  22.     this.left= null;
  23.     this.right=null;
  24.     this.val=val;
  25. }
  26.  
  27. Tree.prototype.addChild = function(val) {
  28.     if (this.val > val) {
  29.         if (this.left === null) {
  30.             this.left= new Tree(val);
  31.         } else {
  32.             this.left.addChild(val);
  33.         }
  34.     }
  35.     if (this.val < val) {
  36.         if (this.right === null) {
  37.             this.right= new Tree(val);
  38.         } else {
  39.             this.right.addChild(val);
  40.         }
  41.     }
  42. }
  43.  
  44. // FIRST LEVEL STARTS AT LEVEL 1
  45. const findLargestLevel = function(node) {
  46.     // your code here
  47.     var arrayOfValues = [];
  48.     var queued = new Queue;
  49.     node.currentLevel = 0;
  50.     queued.enqueue(node);
  51.     var currentNode;
  52.     var currentLevel = 0;
  53.     while (queued.size() > 0) {
  54.         currentNode = queued.dequeue();
  55.         // keep track of the current level
  56.         if (currentNode.currentLevel > currentLevel) {
  57.             currentLevel++;
  58.         }
  59.         // adds val to current sum of vals at current level
  60.         if (arrayOfValues[currentLevel] === undefined) {
  61.           arrayOfValues[currentLevel] = currentNode.val;
  62.         } else {
  63.           arrayOfValues[currentLevel] += currentNode.val;
  64.         }
  65.         if (currentNode.left !== null) {
  66.             currentNode.left.currentLevel = currentNode.currentLevel+1;
  67.             queued.enqueue(currentNode.left);
  68.         }
  69.         if (currentNode.right !== null) {
  70.             currentNode.right.currentLevel = currentNode.currentLevel+1;
  71.             queued.enqueue(currentNode.right);
  72.         }
  73.     }
  74.     console.log(arrayOfValues)
  75.     return arrayOfValues.indexOf(Math.max(...arrayOfValues))+1;
  76.   };
  77. // FIRST LEVEL STARTS AT LEVEL 1
  78.  
  79.  
  80. //   var test = new Tree(0)
  81. //   test.addChild(1)
  82.  
  83. //   test.addChild(-2)
  84. //   test.addChild(6)
  85. //   test.addChild(-1)
  86. //   test.addChild(-4)
  87.  
  88. //   findLargestLevel(test)
  89.  
  90.  
  91.  
  92.  
  93. // //   0 .   0
  94. // // -2  1.    0
  95. // //-4-1  7  .  1
  96. // //
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top