[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] ...
随机推荐
- android 视频
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 第一套完整版: 第二套完整版: 第三套完整版: 第四套完整版: 第五套完整版: ==== ...
- 线性表之顺序表C++实现
线性表之顺序表 一.头文件:SeqList.h //顺序线性表的头文件 #include<iostream> ; //定义顺序表SeqList的模板类 template<class ...
- MVC 设计模式与三层架构
一.JavaEE开发模式 什么是开发模式 模式是在开发过程中总结出的"套路",总结出的一套约定俗成的设计模式 JavaEE模式 model1模式 技术组成 :jsp+javaBea ...
- bzoj 3283 扩展BSGS + 快速阶乘
T2 扩展BSGS T3 快速阶乘 给定整数n,质数p和正整数c,求整数s和b,满足n! / pb = s mod pc 考虑每次取出floor(n/p)个p因子,然后将问题转化为子问题. /*** ...
- hdu 4442 Physical Examination 贪心排序
Physical Examination Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Uva 5002 - The Queue DFS
On some special occasions Nadia’s company provide very special lunch for all employees of the compan ...
- OPENCV----在APP性能测试中的应用(一)
应用项目: APP的性能测试 应用场景: APP启动速度 视频开播速度 加载速度 等~~ 缘来: 基于APP日志和UiAutomator的测试方案,测试结果不能直白且精确的反应,用户的体验 ...
- CentOS 7 下编译安装lnmp之MySQL篇详解
一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:centos-release-7-5.1804.el7.centos.x86_64 二.MySQL下载 MySQL ...
- MySQL运维开发相关的所有工具
http://www.ruzuojun.com/topic/592.html Percona Toolkit 安装使用 http://cenalulu.github.io/mysql/mysql- ...
- 3D数学读书笔记——3D中的方位与角位移
本系列文章由birdlove1987编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/25339595 方位和角位移 ...