[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%:会强制将元素变成和父元素一样的宽,并且添加额外 ...
随机推荐
- 多个结果集union后保持各自原有排序
SELECT *FROM ( SELECT TOP (@count1) a.* FROM Article AS a WITH (NOLOCK)LEFT JOIN Article_Type AS at ...
- Office 2016、2019 与 Office 365 的区别
点开观看更清晰:
- 还是a+b
题目描述: 给定 2 个正整数 a, b,a 和 b 最多可能有 40 位,求出 a + b 的和.输入描述: 两个正整数 a, b,a 和 b 最多可能有 40 位.一行表示一个数.输出描述: a ...
- SAS学习笔记4 基本运算语句(lag、retain、_n_函数)
lag:返回的是上一次lag函数运行时的实参,即lag(argument)=上一次lag函数执行时的argument retain:对变量进行值的初始化和保留到下一个迭代步 _n_:data步的自动变 ...
- 怎样对小数进行向上取整 / 向下取整 / 四舍五入 / 保留n位小数 / 生成随机数
1. 向上取整使用: Math.ceil() Math.ceil(0.1); Math.ceil(1.9); 2. 向下取整使用: Math.floor() Math.floor(0.1); Math ...
- 整理下线段树吧 poj hotel
除了上次的新学的有 区间更新 延迟更新 区间合并 先说下区间更新以及延迟更新吧 既然是对区间的维护 在求解一些问题的时候 有的时候没有必要对所有的自区间都进行遍历 这个时候 延迟标记就派上用场了 ( ...
- tint2
#---------------------------------------------# TINT2 CONFIG FILE#---------------------------------- ...
- Django rest-framework框架-组件之路由
路由: a. url(r'^(?P<version>[v1|v2]+)/v1/$',views.ViewView.as_view()) url(r'^(?P<version>[ ...
- SIP:100rel 扩展
SIP:100rel 扩展 100rel扩展即是对中间状态响应的确认(即1xx的响应码).原先在sip里,只有针对invite请求的200ok响应才会有ack,那么当中间状态响应携带重要的会话参数信息 ...
- C#基础 结构体、枚举
一 结构体 结构体(struct)指的是一种数据结构,一个变量组,是一个自定义的集合.通常使用结构体创造新的“属性”,封装一些属性来组成新的类型. 结构体一般定义在Mian函数上面,位于Class ...