
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding Sum of Left Leaves of a BST in JavaScript
Problem
We are required to write a JavaScript function that takes in the root of a Binary Search Tree as the only argument.
The function should simply calculate the sum of data stored in the left leaves of the BST.
For example, if the Tree looks like this −
8 / \ 1 10 / \ 5 17
Then the output should be −
const output = 6;
Output Explanation:
Because there are two left leaves in the Tree with values 1 and 5.
Example
The code for this will be −
class Node{ constructor(data) { this.data = data; this.left = null; this.right = null; }; }; class BinarySearchTree{ constructor(){ // root of a binary seach tree this.root = null; } insert(data){ var newNode = new Node(data); if(this.root === null){ this.root = newNode; }else{ this.insertNode(this.root, newNode); }; }; insertNode(node, newNode){ if(newNode.data < node.data){ if(node.left === null){ node.left = newNode; }else{ this.insertNode(node.left, newNode); }; } else { if(node.right === null){ node.right = newNode; }else{ this.insertNode(node.right,newNode); }; }; }; }; const BST = new BinarySearchTree(); BST.insert(5); BST.insert(3); BST.insert(6); BST.insert(6); BST.insert(9); BST.insert(4); BST.insert(7); const isLeaf = node => { if (!node) return false; return (node.left === null && node.right === null); } const traverseTreeAndSumLeftLeaves = (root, sum = 0) => { if (!root) return sum; if (isLeaf(root)) return sum; if (root.left) { if (isLeaf(root.left)) { sum += root.left.data; traverseTreeAndSumLeftLeaves(root.left, sum); } else sum = traverseTreeAndSumLeftLeaves(root.left, sum); } if (root.right) { if (isLeaf(root.right)) return sum; else { sum = traverseTreeAndSumLeftLeaves(root.right, sum); } } return sum; }; console.log(traverseTreeAndSumLeftLeaves(BST.root));
Output
The output in the console will be −
7
- Related Articles
- Finding sum of multiples in JavaScript
- Find sum of all left leaves in a given Binary Tree in C++
- Finding sum of a range in an array JavaScript
- Finding lunar sum of Numbers - JavaScript
- Finding sum of all numbers within a range in JavaScript
- Finding sum of all unique elements in JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding sum of alternative elements of the array in JavaScript
- Finding sum of sequence upto a specified accuracy using JavaScript
- Finding desired sum of elements in an array in JavaScript
- Finding the sum of unique array values - JavaScript
- Finding closest pair sum of numbers to a given number in JavaScript
- Finding the sum of floors covered by an elevator in JavaScript
- Finding least number of notes to sum an amount - JavaScript
- Finding the sum of minimum value in each row of a 2-D array using JavaScript

Advertisements