[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 消息机制,handler机制,messageQueue,looper
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha handler 就是 处理器 . 用来处理消息, 发送消息. handler 就 ...
- wpf企业应用之SelectButton(用于列表页之类的选择)
在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中.这里我给自己的控件命名为SelectButton,具体效果见 wpf企业级开发中的几种常见业 ...
- bzoj4668: 冷战 并查集按秩合并
题目链接 bzoj4668: 冷战 题解 按秩合并并查集,每次增长都是小集合倍数的两倍以上,层数不超过logn 查询路径最大值 LCT同解 代码 #include<bits/stdc++.h&g ...
- 王彪-20162321《程序设计与数据结构2nd》-第十一周学习总结与实验报告
邻接矩阵实现 例图 分析 变量 需要一个链表来保存数据-即保存结点 需要一个二维数组来保存每个变得权值,有则填入具体数值,没有则用0 定义一个保存边个数的值 函数方法 得到图中边的个数 得到结点的数据 ...
- hdoj 1002 A + B Problem II 高精度 java
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 记ie8及以下版本ie的flash的addCallback的一坑
近来有一需求,播放声音,我在高端浏览器实现了html5 audio标签.低端浏览器实现了flash兼容.但是在调试ie8以下的浏览器发现js死活调不了flash里的addCallback的方法,总报错 ...
- 工作记录(1)- js问题
也是好久不写博客了,确实懒了:想想应该把node.js的东西写完整比较好,在抽时间吧: 这几天在做阿里巴巴的一个页面展示,里面设计到了一些js的问题,中途也遇到了一些幼稚的问题, 算是简单记录一下,以 ...
- Window 8 启用 Telnet 命令工具一览图
Window 8 启用 Telnet 命令工具一览图 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创 ...
- C++ Primer 学习笔记_34_STL实践与分析(8) --引言、pair类型、关联容器
STL实践与分析 --引言.pair类型.关联容器 引言: 关联容器与顺序容器的本质差别在于:关联容器通过键[key]来存储和读取元素,而顺序容器则通过元素在容器中的位置顺序的存取元素. ma ...
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver解决方式
昨天整理桌面的时候将桌面的一堆文件移动到F盘去了,结果导致原来建的一些项目名称所有出现红色感叹号,打开一看,原来是由于我把hibernate的那些jar包移走了.导致user library里那些ja ...