// --- 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的更多相关文章

  1. JQuery获取元素宽度.width()与.css(‘width’)两个函数的区别

    整理翻译自:http://blog.jquery.com/2012/08/16/jquery-1-8-box-sizing-width-csswidth-and-outerwidth/ 大意是: 在J ...

  2. 区分width()、css('width')、innerWidth()

    #widthTest1 { width: 200px; height: 200px; background-color: #00CCFF; -webkit-box-sizing: border-box ...

  3. css中width:auto和width:100%的区别是什么

    width的值一般是这样设置的: 1,width:50px://宽度设为50px 2,width:50%://宽度设为父类宽度的50% 3,还有一个值是auto(默认值),宽度是自动的,随着内容的增加 ...

  4. CSS的width:100%和width:auto区别

    CSS的width:100%和width:auto区别 一.   问题 前段时间在调整树结构的时候,发现如果树的节点名称比较长的话在IE6下则不会撑开外面的元素,导致节点的名称只显示了一半,同时图标和 ...

  5. table width 决定 td width

    w td width 有无在chrome edge ff 均未影响td实际宽度,td接近等比分配table width. <!doctype html> <html lang=&qu ...

  6. width:auto 和 width:100%有什么区别

    width:auto 和 width:100%有什么区别 宽度计算不同 <div class="parent"> <span class="child& ...

  7. width:100%;与width:auto;的区别

    <div> <p>1111</p> </div> div{ width:980px; background-color: #ccc; height:30 ...

  8. css width="100" style ="width:100px" 区别

    1. width="100"是正确的,而 width="100px"是错误的, style = "width:100px"是正确的 2. s ...

  9. width:auto; 和 width:100%;的不同

    width:auto:会将元素撑开至整个父元素width,但是会减去子节点自己的margin,padding或者border的大小.width:100%:会强制将元素变成和父元素一样的宽,并且添加额外 ...

随机推荐

  1. C++视频读取与视频保存

    VideoCapture cap("E:\\122.avi"); //计算视频帧数 int VedioFPS = cap.get(CV_CAP_PROP_FPS); //cout ...

  2. 导出excel的功能效果实现

    <el-button @click="exportExcel" > <i style="display: inline-block;"> ...

  3. shell习题第22题:

    [题目要求] 加入A服务器可直接ssh到B,不用输入密码.A和B都有一个目录是/data/web/这下有很多文件,我们不知道这下面有多少层目录,但是之前的目录结构和文件是一模一样的.但是现在不确定是否 ...

  4. linux下通过vim编辑文件的方法

    一般来说是通过指令进入文件的编辑页面: vi [filename] 此时进入的是一般指令模式,然后可以直接移动光标对内容进行修改. 修改完成后,使用Esc 按键退出编辑模式. 此时回到的还是一般指令模 ...

  5. Python练习_数据类型_day4

    题目 1.作业 1,写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", " ...

  6. A*算法与8数字谜题(参见《算法》P226习题2.5.32)

    A*算法的目的是找到一条从起始状态到最终状态的最短路径. 在A*算法中,需要在每个点计算启发函数:f(S) = g(S) + h(S),其中g(S)是从起点到S点的距离,h(S)是对从S点到终点的最短 ...

  7. Android中ListView的使用

    1.主要概念 ListView用于将大数据集以列表的形式展示. ListView可以看成一个容器,它有如下继承链: View <- ViewGroup <- AdapterView < ...

  8. win10下PLSQL Developer 连接ubuntu上安装的oracle 11g

    说明:过程记录的不是很相信,只记录基本步骤.并不适合想一步一步照做的同学. win10下需要的操作 1.微软官网下载instantclient,然后接到到本地一个文件夹,注意路径不要又空格,中文和括号 ...

  9. angular轮播图

    还是直接上代码比较好 <!doctype html><html lang="en"><head> <meta charset=" ...

  10. python matplotlib以日期为x轴作图

    from datetime import datetime, date, timedelta import matplotlib.pyplot as plt import tushare as ts ...