[Algorithm] Binary tree: Level Order Traversal

function Node(val) {
return {
val,
left: null,
right: null
};
}
function Tree() {
return {
root: null,
addLeft(val, root) {
const newNode = Node(val);
root.left = newNode;
return newNode;
},
addRight(val, root) {
const newNode = Node(val);
root.right = newNode;
return newNode;
},
levelOrder(root, visitFn) {
const helper = (node, visitFn) => {
if (node === null) {
return;
}
const q = new Queue();
q.push(node);
while (!q.isEmpty()) {
const current = q.peak();
visitFn(current.val);
current.left && q.push(current.left);
current.right && q.push(current.right);
q.pop();
}
};
helper(root, visitFn);
}
};
}
function Queue() {
return {
nodes: [],
push(val) {
this.nodes.push(val); // O(1)
},
pop() {
const [first, ...rest] = this.nodes;
this.nodes = rest;
return first;
},
peak() {
return this.isEmpty() ? null : this.nodes[0];
},
isEmpty() {
return this.nodes.length === 0;
}
};
}
/**
20
14 28
10 15 24 32
4 11 21
*/
const tree = new Tree();
const n1 = Node(20);
tree.root = n1;
const n2 = tree.addLeft(14, n1);
const n3 = tree.addRight(28, n1);
const n4 = tree.addLeft(10, n2);
tree.addRight(15, n2);
const n5 = tree.addLeft(24, n3);
tree.addRight(32, n3);
tree.addLeft(4, n4);
tree.addRight(11, n4);
tree.addLeft(21, n5);
tree.levelOrder(tree.root, x => console.log(x));
//20,14,28,10,15,24,32,4,11,21
[Algorithm] Binary tree: Level Order Traversal的更多相关文章
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- lettcode-102:Binary Tree Level Order Traversal (Java)
Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...
- Binary Tree Level Order Traversal II 解题思路
思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...
随机推荐
- Windows Sysinternals实战指南
http://www.epubit.com.cn/book/details/4786 Mark Russinovich是Microsoft Azure首席技术官,主要负责微软云计算平台的技术战略和架构 ...
- CVPR 2017
https://www.leiphone.com/news/201707/5D5qSICrej6xIdzJ.html Densely Connected Convolutional Networks ...
- [Asp.net core 2.0]Ueditor 图片上传
摘要 在项目中要用到富文本编辑器,包含上传图片,插入视频等功能.但ueditor只有.net版本,没有支持core.那么上传等接口就需要自己实现了. 一个例子 首先去百度ueditor官网下载简化版的 ...
- Ubuntu 14.04 用户如何安装 VLC 2.2.0
http://www.linuxidc.com/Linux/2014-03/98913.htm http://www.videolan.org/vlc/#download VLC 是一款自由开源.跨平 ...
- Linux学习7-tomcat部署多个项目(多个端口)
前言 前面已经在tomcat上搭建了jenkins的环境,如果我们有多个项目需要部署的话,如何在一个tomcat下部署多个项目呢? 前面是直接在:8080/jenkins访问的,如果有其它项目部署的话 ...
- Java实现用汉明距离进行图片相似度检测的
Google.Baidu 等搜索引擎相继推出了以图搜图的功能,测试了下效果还不错~ 那这种技术的原理是什么呢?计算机怎么知道两张图片相似呢? 根据Neal Krawetz博士的解释,原理非常简单易懂. ...
- 《iOS 7 应用开发实战详解》
<iOS 7 应用开发实战详解> 基本信息 作者: 朱元波 管蕾 出版社:人民邮电出版社 ISBN:9787115343697 上架时间:2014-4-25 出版日期:2014 年5 ...
- .NET零基础入门06:面向对象入门
一:前言 在本系列课程的第一部分,我们说明为了要选择C#作为你成为程序员的第一门语言: • 首先,C#是一门非常优秀的面向对象编程的语言: 凡是对编码感兴趣的同学一定听说过"面向对象编程&q ...
- Java语法糖初探(三)--变长参数
变长参数概念 在Java5 中提供了变长参数(varargs),也就是在方法定义中可以使用个数不确定的参数,对于同一方法可以使用不同个数的参数调用.形如 function(T …args).但是需要明 ...
- GPS定位基本原理浅析
位置服务已经成为越来越热的一门技术,也将成为以后所有移动设备(智能手机.掌上电脑等)的标配.而定位导航技术中,目前精度最高.应用最广泛的,自然非GPS莫属了.网络上介绍GPS原理的专业资料很多,而本文 ...