[Algorithm] Tree Width with Level Width
// --- Directions
// Given the root node of a tree, return
// an array where each element is the width
// of the tree at each level.
// --- Example
// Given:
//
// / | \
// 1 2 3
// | |
// 4 5
// Answer: [1, 3, 2]
function levelWidth(root) {
let counts = [0];
let levels = [root, "$end$"];
// [1, 3, 2]
// ["e"]
while (levels.length > 1) {
const current = levels.shift();
if (current === "$end$") {
counts[counts.length] = 0;
levels.push("$end$");
} else {
const { children } = current;
for (let node of children) {
levels.push(node);
}
counts[counts.length - 1]++;
}
}
return counts;
}
module.exports = levelWidth;
Test:
const Node = require('./node');
const levelWidth = require('./index');
test('levelWidth is a function', () => {
expect(typeof levelWidth).toEqual('function');
});
test('levelWidth returns number of nodes at widest point', () => {
const root = new Node(0);
root.add(1);
root.add(2);
root.add(3);
root.children[0].add(4);
root.children[2].add(5);
expect(levelWidth(root)).toEqual([1, 3, 2]);
});
test('levelWidth returns number of nodes at widest point', () => {
const root = new Node(0);
root.add(1);
root.children[0].add(2);
root.children[0].add(3);
root.children[0].children[0].add(4);
expect(levelWidth(root)).toEqual([1, 1, 2, 1]);
});
[Algorithm] Tree Width with Level Width的更多相关文章
- JQuery获取元素宽度.width()与.css(‘width’)两个函数的区别
整理翻译自:http://blog.jquery.com/2012/08/16/jquery-1-8-box-sizing-width-csswidth-and-outerwidth/ 大意是: 在J ...
- 区分width()、css('width')、innerWidth()
#widthTest1 { width: 200px; height: 200px; background-color: #00CCFF; -webkit-box-sizing: border-box ...
- css中width:auto和width:100%的区别是什么
width的值一般是这样设置的: 1,width:50px://宽度设为50px 2,width:50%://宽度设为父类宽度的50% 3,还有一个值是auto(默认值),宽度是自动的,随着内容的增加 ...
- CSS的width:100%和width:auto区别
CSS的width:100%和width:auto区别 一. 问题 前段时间在调整树结构的时候,发现如果树的节点名称比较长的话在IE6下则不会撑开外面的元素,导致节点的名称只显示了一半,同时图标和 ...
- table width 决定 td width
w td width 有无在chrome edge ff 均未影响td实际宽度,td接近等比分配table width. <!doctype html> <html lang=&qu ...
- width:auto 和 width:100%有什么区别
width:auto 和 width:100%有什么区别 宽度计算不同 <div class="parent"> <span class="child& ...
- width:100%;与width:auto;的区别
<div> <p>1111</p> </div> div{ width:980px; background-color: #ccc; height:30 ...
- css width="100" style ="width:100px" 区别
1. width="100"是正确的,而 width="100px"是错误的, style = "width:100px"是正确的 2. s ...
- width:auto; 和 width:100%;的不同
width:auto:会将元素撑开至整个父元素width,但是会减去子节点自己的margin,padding或者border的大小.width:100%:会强制将元素变成和父元素一样的宽,并且添加额外 ...
随机推荐
- 第三坑:jar包编译版本
这个是之前往was上发应用的时候踩的一个坑,当时我们知道was的jdk版本是1.6,然后我们是用1.7的jdk,编译版本选的是1.6,然后放上去不对,我们以为是编译的问题,然后又下载了1.6的jdk, ...
- EasyUI 对话框弹出文件输入框
目前用的EasyUI的dialog,要实现弹出文件输入框(或者其他输入框和对话框),我的实现方案是,首先写一个close的div,然后里面就是样式和输入框的一些代码和一个确定按钮,然后页面上一个按钮, ...
- 【坑】springMvc 信息校验,读取不到错误配置信息的问题
文章目录 前言 ResourceBundleMessageSource 后记 前言 springMvc 的一大利器,validation 检验,通过注解,可以帮我们完成校验,很是顺手. 终极偷懒检验, ...
- ndarray笔记续
数组的索引与切片 多维数组的索引 import numpy as np arr=np.arange(1,25).reshape(2,3,4) arr # 输出 array([[[ 1, 2, 3, 4 ...
- 使用QSaveFile类安全的读写文件(继承自QFileDevice,与QFile并列)
QSaveFile类也是一种I/O设备,来用来读写文本文件和二进制文件,但使用该类的话,在写入操作失败时不会导致已经存在的数据丢失. 该类在执行写操作时,会先将内容写入到一个临时文件中,如果没有错误发 ...
- Tokitsukaze and Strange Rectangle CodeForces - 1191F (树状数组,计数)
大意: 给定$n$个平面点, 定义集合$S(l,r,a)$表示横坐标$[l,r]$纵坐标$[a,\infty]$内的所有点. 求可以得到多少种不同的集合. 从上往下枚举底层最右侧点, 树状数组统计贡献 ...
- 不遮挡人物弹幕是怎么实现的——图片蒙版效果-webkit-mask
这是一个实验中的功能,用于设置元素上遮罩层的图像. 一.Values none:默认值,透明的黑色图像层,也就是没有遮罩层. <mask-source>:<mask>或CSS图 ...
- 中国5级行政区域MySQL数据库库
爬取国家统计局官网的行政区域数据,包括省市县镇村5个层级,其中港澳地区的数据只有3级;台湾地区4级;包含大陆地区的邮政编码和经纬度信息. 行政划分的总体结构是: 省->市(州)->县(区) ...
- Linux环境下Redis安装和常见问题的解决
Redis是一款非常非常非常好用的缓存数据库,在保存需要定期更新的Token值,或者在保证高并发安全可靠性的场景下,基本上说是必用了. 安装过好多次Redis了,也用过阿里云Redis数据库,但是每次 ...
- 5.Redis的持久化
Redis中数据的持久化有两种方式:RDB(Redis DataBsse) 和 AOF(Append Only File),默认采取的是RDB方式 RDB 1.是什么:在指定的时间间隔内将内存中的数据 ...