[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();
root.left = Node();
root.right = Node();
root.right.left = Node();
root.right.right = Node();
root.right.left.left = Node();
root.right.left.right = Node();
function count_unival(root) {
  function helper(root) {
    let total_count = ;
    let is_unival = true;
    // Base case 1: if current node is null, then return
    if (root == null) {
      return [, 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 [, 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 + , 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[] ? "" : "n't"
  } unival tree, total counts for sub-unival tree is ${res[]}`
); // Whole tree isn't unival tree, total count is 5
[Algorithm] Universal Value Tree Problem的更多相关文章
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
		
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
 - xtu数据结构 I. A Simple Tree Problem
		
I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld ...
 - ZOJ 3686 A Simple Tree Problem
		
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
 - 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
		
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
 - ZOJ-3686 A Simple Tree Problem 线段树
		
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...
 - Teemo's tree problem
		
题目链接 : https://nanti.jisuanke.com/t/29228 There is an apple tree in Teemo's yard. It contains n node ...
 - ZOJ 3686 A Simple Tree Problem(线段树)
		
Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...
 - [Algorithm] 1. A+B Problem
		
Description Write a function that add two numbers A and B. Clarification Are a and b both 32-bit int ...
 - [Algorithm] 94. Binary Tree Inorder Traversal iteratively approach
		
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
 
随机推荐
- [BZOJ5292][BJOI2018]治疗之雨(概率DP+高斯消元)
			
https://blog.csdn.net/xyz32768/article/details/83217209 不难找到DP方程与辅助DP方程,发现DP方程具有后效性,于是高斯消元即可. 但朴素消元显 ...
 - [Agc002E]Candy Piles
			
[Agc002E]Candy Piles 题目大意 有\(n\)个数,两人轮流操作,可以做以下操作之一: 删掉一个最大的数 将所有数-1 最后取没的人输,问先手是否必胜? 试题分析 直接决策不知道选哪 ...
 - hdu 3338 最大流 ****
			
题意: 黑格子右上代表该行的和,左下代表该列下的和 链接:点我 这题可以用网络流做.以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变.求满足的最大流.由于 ...
 - bzoj 1563
			
对于很多决策单调性DP问题,我们很难(但不是不可以)证明其决策满足单调性,所以感觉很像时,可以打表看是否满足. 这道题的精度(?范围)很难搞,开始生怕溢出,看了hzwer的代码,才发现用long do ...
 - TSQL update 简单应用小总结
			
UPDATE 有两种基本的格式.一种是用静态数据来修改表,另一种是用其他表中的数据来修改表.下面是第一种格式: UPDATE #famousjaycees SET jc = 'Jhony cash', ...
 - HDU 5694 BD String 迭代
			
BD String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5694 Description Problem Description 众所周知, ...
 - JS简单实现二级联动菜单
			
<form method="post" action=""> 省/市:<select id="province" onch ...
 - GCC 对C语言的扩展
			
http://www.cnblogs.com/emituofo/archive/2012/07/20/2600995.html http://blog.csdn.net/andyhuabing/art ...
 - mysql 3.2.49 源代码安装-redhat 5 x64
			
[mysql@localhost ~]$ uname -r2.6.32 [root@localhost ~]#cp /usr/include/pthread.h /usr/include/pthrea ...
 - MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传
			
本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 ...