[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%:会强制将元素变成和父元素一样的宽,并且添加额外 ...
随机推荐
- lubuntu踩坑全记录
为了降低系统占用,毕业之后一直用lubuntu不用ubuntu...操作其实差不多,就是lubuntu有一些小坑坑:P 本文是我的踩坑全记录.长期更新. 调分辨率 升级命令lubuntu不出登录页面 ...
- python并发编程之多线程(实践篇)
一.threading模块介绍 官网链接:https://docs.python.org/3/library/threading.html?highlight=threading# 1.开启线程的两种 ...
- Scratch教程:谁是真悟空
在西游记中,有一集是“真假悟空”,六耳猕猴变成了悟空的模样与真悟空真假难辨,打的不可开交. 在Scartch中,我们常常会使用一个本体来生成多个克隆体,这在开发过程中有重要的意义.但在实际操作中,每个 ...
- 剑指offer(9)——用两个栈实现队列
题目: 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 思路: 首先定义两个栈stack1. ...
- docker相关--dockerd日志设置
背景 线上容器dockerd的后台程序打印了超过几十G的日志 Docker daemon日志的位置: Docker daemon日志的位置,根据系统不同各不相同. Ubuntu - /var/log/ ...
- 前端 aes 加密
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 无法访问VMWARE虚拟机中linux的nginx地址
取得root权限,然后在centos(6.5)中关闭防火墙: service iptables stop 关闭后在windows 10 中浏览器通过虚拟机的ip地址可以直接访问了. 经过测试,打开ip ...
- Form key length limit 2048 exceeded ,提交数据时,数据的键过长 或者是上传文件过大
在ASP.NET Core MVC中,文件的key 默认最大为2048,文件上传的最大上传文件默认为20MB,如果我们想上传一些比较大的文件,就不知道怎么去设置了,没有了Web.Config我们应该如 ...
- log4j2.xml配置,导致启动报错
项目中遇到问题,当使用tomcat启动时,没问题:当使用内置tomcat启动时却报错,找不到日志路径. 变量位置: <properties> <property name=" ...
- Spring IOC原理分析
IOC IOC(Inversion of Control)控制反转:所谓的控制反转,就是把原先需要我们代码自己实现对象的创建和依赖,反转给容器来实现.那么必然Spring需要创建一个容器,同时需要创建 ...