[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] ...
随机推荐
- PHP 笔记——自定义函数
1. 定义函数 function function_name ([$arg_1],[$arg_2], ... [$arg_n]){ fun_body; [return arg_n;] } 在PHP中, ...
- MAC下安装多版本JDK和切换几种方式
环境: MAC AIR,OS X 10.10,64位 历史: 过去 Mac 上的 Java 都是由 Apple 自己提供,只支持到 Java 6,并且OS X 10.7 开始系统并不自带(而是可选 ...
- 细说React(一)
React 是近期非常热门的一个前端开发框架. 这篇文章将介绍如何使用 React 来创建用户界面,希望能够起到抛砖引玉的效果. "React, A JAVASCRIPT LIBRARY ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- ROS知识(13)----基于catkin的包安装
ROS软件包开发完成后,需要安装包,如果你用的是catkin创建的工作空间,那么即可使用命令"catkin_make install"完成此项任务.下面介绍其安装的过程: 1.源码 ...
- vue2.0使用记录
父组件给子组件传值[props] 1.首先在父组件的script标签中引入子组件 import Children from './Children' 2.在template内引入子组件 <Chi ...
- NGINX 如何防盗链
一.安装Nginx: 1.解决依赖关系 # yum groupinstall "Development Tools" "Server Platform Deveopmen ...
- 安装oracle 11gr2 rac on solaris
http://candon123.blog.51cto.com/704299/389470
- 使用Bootstrap 3开发响应式网站实践06,使用ListGroup、Thumbnails展示内容
□ ListGroup展示内容 当希望把同类型的内容以列表.区块展示的时候,ListGroup是不错的选择. <div class="col-sm-6"> <h3 ...
- MVC扩展生成CheckBoxList并水平排列
本篇体验生成CheckBoxList的几个思路,扩展MVC的HtmlHelper生成CheckBoxList,并使之水平排开. 通过遍历从控制器方法拿到的Model集合 □ 思路 比如为一个用 ...