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的更多相关文章

  1. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

    A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

  2. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  3. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  4. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  5. ZOJ-3686 A Simple Tree Problem 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...

  6. Teemo's tree problem

    题目链接 : https://nanti.jisuanke.com/t/29228 There is an apple tree in Teemo's yard. It contains n node ...

  7. 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 ...

  8. [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 ...

  9. [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] ...

随机推荐

  1. 破解神器Hashcat使用简介

    0x00 背景 目前GPU的速度越来越快,使用GPU超强的运算速度进行暴力密码破解也大大提高了成功率,曾经看到老外用26块显卡组成的分布式破解神器让我羡慕不已.要说目前最好的GPU破解HASH的软件, ...

  2. hdu 4535 错排

    题意:错排 链接:点我 百年难得一遇大水题 #include<cstdio> #include<iostream> #include<algorithm> #inc ...

  3. Codeforces Round #256 (Div. 2) C. Painting Fence

    C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Ch ...

  4. hihocoder #1015 KMP

    #include<stdio.h> #include<iostream> #include<math.h> #include<string.h> usi ...

  5. dwr.jar简介

    DWR(Direct Web Remotiong)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架, 可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运 ...

  6. Lucene新版本号对ConjunctionScorer的优化

    Lucene 4.0版本号的DocIdSetIterator中没有cost方法,而4.7.0则有这种方法,表示遍历整个DocIdSet的代价,对于DocsEnum就是其长度了,对于Scorer就能够是 ...

  7. [Apache] Apache 從 2.2 換至 2.4 httpd.conf 的調整筆記 (windows 環境)

    原文地址: http://www.dotblogs.com.tw/maplenote/archive/2012/07/20/apache24_httpd_conf.aspx 整理一下 Windows ...

  8. appium+python自动化59-appium命令行参数

    Appium服务器参数 许多Appium 1.5服务器参数已被弃用,以支持--default-capabilities标志. 用法: node . [flags] help 1.cmd端口输入,app ...

  9. 判断一个请求是否为Ajax请求

    这几天在写一个网站的登录判断拦截器,需要对请求进行拦截,在拦截器中我需要判断HttpServletRequest是否为Ajax异步请求.我们可以通过X-Requested-With="XML ...

  10. Spring3.2.3+Quartz2.2.1 整合配置

    步骤: 1.下载相关包 quartz-2.2.1.jar quartz-jobs-2.2.1.jar spring相关jar包 2.编写配置文件静态 <bean id="activat ...