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. WinForm 使用 NPOI 2.2.1从datatable导出Excel

    最新的NOPI应该是2.3了,但在官网上还是2.2.1. 也是第一次使用NPOI来导出Excel文件. 在写的时候搜不到2.2.1的教程,搜了一个2.2.0的教程. 不过也没什么问题,NPOI是真的方 ...

  2. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS

    B. Om Nom and Dark Park Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  3. Codeforces Round #279 (Div. 2) B - Queue 水题

    #include<iostream> #include<mem.h> using namespace std; ],q[]; int main() { int n,x,y; m ...

  4. Android 通话记录分析

    http://stackoverflow.com/questions/6786666/how-do-i-access-call-log-for-android http://android2011de ...

  5. 3.3V与5V电平双向转换

    http://www.amobbs.com/thread-5541344-1-2.html 3.3V转5V:S3输入为0V时,NMOS管导通,S5=S3=0V:S3输入为3.3V时,NMOS管截止,S ...

  6. IT程序猿们,我该做什么选择呢

    这个时刻,我想我遇到人生小拐点了,程序猿到了30岁,到达了一个分界线了,现在的我该何去何从呢? 先谈下简单的情况吧: 来这个公司2年了,之前因为身体的原因,不想那么累,于是选择了一份维护的工作,就来了 ...

  7. iOS中bundle的意义

    什么是bundle? bundle就是一个文件夹,按照一定标准组织的目录结构.每个iOS APP至少有一个main bundle,这个main bundle包含了app的二进制代码及任何你用到的资源, ...

  8. 给.DLL文件加一个数字签名的方法

     给.dll文件加一个数字签名的方法 效果如图所示:   做法: 下载数字签名工具包:http://files.cnblogs.com/babyt/SignTool.rar  /Files/JavaC ...

  9. Remon Spekreijse CSerialPort串口类的修正版2014-01-10

    转自:http://m.blog.csdn.net/blog/itas109/18358297# 2014-1-16阅读691 评论0 如需转载请标明出处:http://blog.csdn.net/i ...

  10. Oracle 快速插入1000万条数据的实现方式

    1.使用dual配合connect by level create table BigTable as select rownum as id from dual connect by level & ...