[Algorithm] Universal Value Tree Problem

A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.

Given the root to a binary tree, count the number of unival subtrees.

For example, the following tree has 5 unival subtrees:

   0
  / \
 1   0
    / \
   1   0
  / \
 1   1
复制代码
function Node(val) {
  return {
    val,
    left: null,
    right: null
  };
}

const root = Node(0);
root.left = Node(1);
root.right = Node(0);
root.right.left = Node(1);
root.right.right = Node(0);
root.right.left.left = Node(1);
root.right.left.right = Node(1);

function count_unival(root) {
  function helper(root) {
    let total_count = 0;
    let is_unival = true;

    // Base case 1: if current node is null, then return
    if (root == null) {
      return [0, true];
    }

    // Base case 2: if current node is not null, but its children node
    // are null, then count this node as usb-unvial tree
    if (root.left === null && root.right === null) {
      return [1, true];
    }

    // Base case 1 & Base case 2 can keep just one, it should still works

    // Do the Recursion
    let [left_count, is_left_unival] = helper(root.left);
    let [right_count, is_right_unival] = helper(root.right);

    // we need to consider whether the whole tree
    // root + left tree + right tree are unvial
    // the way to do it just compare root with its left and right node
    // whether they are the same if both left tree and right tree are
    // unival tree.
    if (!is_left_unival || !is_right_unival) {
      is_unival = false;
    }

    if (root.left !== null && root.val !== root.left.val) {
      is_unival = false;
    }

    if (root.right !== null && root.val !== root.right.val) {
      is_unival = false;
    }

    // If the whole tree are unival tree, then the final result
    // should + 1
    if (is_unival) {
      return [left_count + right_count + 1, is_unival];
    } else {
      return [left_count + right_count, is_unival];
    }
  }

  const [total_count, is_unival] = helper(root);
  return [total_count, is_unival];
}

const res = count_unival(root);
console.log(
  `Whole tree is${
    res[1] ? "" : "n't"
  } unival tree, total counts for sub-unival tree is ${res[0]}`
); // Whole tree isn't unival tree, total count is 5
复制代码

 

posted @   Zhentiw  阅读(443)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-03-13 [HTML 5] Atomic Relevant Busy
2018-03-13 [HTML 5] aria-live
2018-03-13 [Node.js] Proxy Requests for Local and Remote Service Parity
2018-03-13 [Node.js] Manage Configuration Values with Environment Variables
2018-03-13 [HTML 5] aria-hidden
2017-03-13 [Ramda] Convert a Promise.all Result to an Object with Ramda's zip and zipObj
2016-03-13 [RxJS] Basic DOM Rendering with Subscribe
点击右上角即可分享
微信分享提示