[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%:会强制将元素变成和父元素一样的宽,并且添加额外 ...
随机推荐
- Spring之23:AbstractBeanFactory,Bean的加载
<spring源码之:循环依赖> AbstractBeanFactory的作用:别名管理,单例创建与注册,工厂方法FactoryBean支持. 由图我们直接的看出,AbstractBean ...
- pythn print格式化输出:%s与%d
pythn print格式化输出. %r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的. 1. 打印字符串 print (" ...
- 在一个form表单中实现多个submit不同的action
在button中用JS的事件绑定onclick实现,如下: <!-- employees是表单的name属性值--> <script type="text/javascri ...
- STM32的I2C特性及架构
软件模拟协议:使用CPU直接控制通讯引脚(GPIO)的电平,产生出符合通讯协议标准的逻辑. 硬件实现协议:由STM32的I2C片上外设专门负责实现I2C通讯协议,只要配置好该外设,它就会自动根据协议要 ...
- CMake入门-01-从HelloWorld开始
工作环境 系统:macOS Mojave 10.14.6 CMake: Version 3.15.0-rc4 从 Hello,World! 开始 (1) 新建 hello 目录,创建文件 CMakeL ...
- 轻松搭建CAS 5.x系列(5)-增加密码找回和密码修改功能
概述说明 CAS内置了密码找回和密码修改的功能: 密码找回功能是,系统会吧密码重置的连接通过邮件或短信方式发送给用户,用户点击链接后就可以重置密码,cas还支持预留密码重置的问题,只有回答对了,才可以 ...
- JDBC 复习2 存取mysql 大数据
大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据 mysql的大数据分为2种 blob 和 text ,没有cl ...
- Python 生成动态变量 调用动态变量
动态生成变量: variable = locals() for i in range(10): variable['A'+str(i)] = 123 print(A8) 调用动态变量: v = loc ...
- perl语言的线程使用
参考的教程链接是 https://www.cnblogs.com/migrantworkers/p/6973459.html 1.Perl 多线程的使用,join 和 detach 的区别 ,join ...
- Hadoop集群搭建(cluster setup),ssh免密后一直要求输入密码的原因
前段时间,网上有言SHA-1加密技术,已经被谷歌公司破解,在linux系统中,集群间加密的技术是用DSA秘钥,秘钥本身其实是一种算法,就像前面说的SHA-1也是加密算法的一种. 免密在linux系统中 ...