Skip to content

Commit

Permalink
JS function to determine how many leaves are in a binary tree
Browse files Browse the repository at this point in the history
  • Loading branch information
TiaLow committed Oct 9, 2020
1 parent 2471a08 commit a3cc859
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions leavesBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

// JavaScript implementation of a function that finds the number of leaves in a binary tree
// function's signature is a binary tree as a parameter, returns the number of leaves in the tree
// relies on the assumption that the input is a binary tree with a root, and made up of nodes with left and right properties on them, as illustrated below
// function uses preOrder depth traversal method

class Node {

constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}

}

class BinaryTree {

constructor( root = null) {
this.root = root;
}

}

function numLeavesInTree(binaryTree) {

let numLeaves = 0;

function _preOrderTraversal(root) {

if(!root) {
return;
}

numLeaves += 1;
_preOrderTraversal(root.left);
_preOrderTraversal(root.right);

}

_preOrderTraversal(binaryTree.root);
return numLeaves;

}

0 comments on commit a3cc859

Please sign in to comment.