Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JavaScript binary tree

// calculate the size of binary tree (number of nodes) 
const sizeTree = (root) => {
  if (!root) return 0;
  let queue = [root];
  let size = 0;

  while (queue.length > 0) {
    let current = queue.shift();

    if (current.left) queue.push(current.left);
    if (current.right) queue.push(current.right);
    size += 1;
  }
  return size;
};
Source by rocoderes.com #
 
PREVIOUS NEXT
Tagged: #JavaScript #binary #tree
ADD COMMENT
Topic
Name
4+1 =