Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding Sum of Left Leaves of a BST in JavaScript
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.
Problem Example
For example, if the Tree looks like this:
8
/ \
1 10
/ \
5 17
Then the output should be:
6
Output Explanation
Because there are two left leaves in the Tree with values 1 and 5, and their sum is 1 + 5 = 6.
Understanding Left Leaves
A left leaf is a node that:
- Is a leaf node (has no children)
- Is the left child of its parent
Solution Approach
We'll use a recursive traversal to identify left leaves and sum their values. The algorithm checks if a node's left child is a leaf, and if so, adds its value to the sum.
Complete Implementation
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
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);
}
}
}
}
// Helper function to check if a node is a leaf
const isLeaf = node => {
if (!node) return false;
return (node.left === null && node.right === null);
}
// Main function to calculate sum of left leaves
const sumOfLeftLeaves = (root) => {
if (!root) return 0;
let sum = 0;
// Check if left child is a leaf
if (root.left && isLeaf(root.left)) {
sum += root.left.data;
}
// Recursively traverse left and right subtrees
sum += sumOfLeftLeaves(root.left);
sum += sumOfLeftLeaves(root.right);
return sum;
}
// Create BST and test
const BST = new BinarySearchTree();
BST.insert(5);
BST.insert(3);
BST.insert(6);
BST.insert(2);
BST.insert(4);
BST.insert(7);
console.log("Sum of left leaves:", sumOfLeftLeaves(BST.root));
Sum of left leaves: 2
How It Works
The algorithm works by:
- Starting from the root node
- For each node, checking if its left child is a leaf
- If the left child is a leaf, adding its value to the sum
- Recursively calling the function on both left and right subtrees
- Returning the total sum
Alternative Approach with Helper Function
const sumOfLeftLeavesHelper = (root, isLeft = false) => {
if (!root) return 0;
// If current node is a leaf and it's a left child
if (isLeaf(root) && isLeft) {
return root.data;
}
// Recursively sum left and right subtrees
return sumOfLeftLeavesHelper(root.left, true) +
sumOfLeftLeavesHelper(root.right, false);
}
// Test with the same BST
console.log("Alternative approach:", sumOfLeftLeavesHelper(BST.root));
Alternative approach: 2
Key Points
- A left leaf must be both a leaf node AND the left child of its parent
- The recursive approach efficiently traverses the entire tree
- Time complexity: O(n) where n is the number of nodes
- Space complexity: O(h) where h is the height of the tree (due to recursion stack)
Conclusion
Finding the sum of left leaves requires identifying nodes that are both leaves and left children of their parents. The recursive traversal approach provides an elegant solution with optimal time complexity.
