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. UOJ275 组合数问题

    给定n,m和k,求有多少对(i , j)满足0 ≤ i ≤ n, 0 ≤ j ≤ min(i ,m)且C(︀i,j)︀是k的倍数.n,m ≤ 1018, k ≤ 100,且k是质数. 把i和j都看成k ...

  2. 【POJ】1835:宇航员【模拟】【三维行走】

    宇航员 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7228   Accepted: 3050 Description 问 ...

  3. Codeforces Round #298 (Div. 2) C. Polycarpus' Dice 数学

    C. Polycarpus' Dice Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/p ...

  4. SGU 406 Goggle

    406. Goggle Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: standardoutput: ...

  5. Python学习笔记(八)—集合的学习

    集合(set)是一个无序不重复元素的序列. 基本功能是进行成员关系测试和删除重复元素. 1.集合的作用 它可以把一个列表中重复的数据去掉,而不需要你再写判断 可以做关系测试,比如说有两个科目,一个数学 ...

  6. js单例模式详解实例

    这篇文章主要介绍了什么是单例单例模式.使用场景,提供了3个示例给大家参考 什么是单例? 单例要求一个类有且只有一个实例,提供一个全局的访问点.因此它要绕过常规的控制器,使其只能有一个实例,供使用者使用 ...

  7. Sublime Text2安装emmet(原名Zen Coding)总结

    首先,安装好Sublime( 我用的是版本号2),之后注冊好.Sublime Text2.0.2注冊码:http://xionggang163.blog.163.com/blog/static/376 ...

  8. IOS学习之基于IOS7的tab bar

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图:      上图中蓝色框 ...

  9. velocity+spring mvc+spring ioc+ibatis初试感觉(与struts+spring+hibernate比较)

    velocity+spring mvc+spring ioc+ibatis框架是我现在公司要求采用的,原因是因为阿里巴巴和淘宝在使用这样的框架,而我公司现在还主要是以向阿里巴巴和淘宝输送外派人员为 主 ...

  10. json-lib包笔记

    json-lib.jar开发包使用: 依赖包:commons-beanutils.jar;commons-httpclient.jar;commons-lang.jar;ezmorph.jar;不少人 ...