Your Submissions
You haven't submitted any code for this challenge yet. Solve the problem by passing all the test cases, and your submissions will appear here.
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.