AlgoDaily Solution
1Locked, only available for premium members.
Community Solutions
Community solutions are only available for premium users.
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.
xxxxxxxxxx
37
var assert = require('assert');
function countUniValueSubtrees(root) {
let count = 0;
if (!root) {
return 0;
}
isUniValHelper(root, count);
return count;
}
function isUniValHelper(node, count) {
// Base case: if both children are null, we know the node is a univalue, we can increment count and return true
if (node.left === null && node.right === null) {
count++;
return true;
}
// Check isUniVal for each child search tree
let isUniVal = true;
if (node.left !== null) {
isUniVal =
isUniValHelper(node.left) && isUniVal && node.left.val === node.val;
}
if (node.right !== null) {
isUniVal =
isUniValHelper(node.right) && isUniVal && node.right.val === node.val;
OUTPUT
Results will appear here.