[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,循 ...
随机推荐
- STM32 USB FS Core and USB OTG Core
STM32 USB-FS-Device development kit Compatible with the STM32F102xx and STM32F103xx series, STM32 L1 ...
- In-Place upgrade to Team Foundation Server (TFS) 2015 from TFS 2013Team Foundation Server TFS TFS 2015 TFS upgrade TFS with Sharepoint
This upgrade document gives detailed step by step procedure for the In-Place upgrade from TFS 2013 t ...
- WCF中修改接口或方法名称而不影响客户端程序
本篇接着"从Web Service和Remoting Service引出WCF服务"中有关WCF的部分. 运行宿主应用程序. 运行Web客户端中的网页. 输入内容,点击按钮,能获取 ...
- JavaScript进阶系列06,事件委托
在"JavaScript进阶系列05,事件的执行时机, 使用addEventListener为元素同时注册多个事件,事件参数"中已经有了一个跨浏览器的事件处理机制.现在需要使用这个 ...
- NSString 和 NSData 转换
NSString 转换成NSData 对象 NSData* xmlData =[@"testdata" dataUsingEncoding:NSUTF8StringEncoding ...
- eval json ajax
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...
- ibatis.net:第三天,Insert
手工生成主键的模型 xml 配置 <insert id="InsertUser" parameterClass="User"> INSERT INT ...
- Unity3D导入3DMax模型缩放单位问题深入分析
“Unity3D导入3DMax制作的模型存在100倍缩放比例”,各Unity3D开发者基本都听过吧. 怎么保证3DMax中制作的1m导入Unity3D后还是1m? 为什么会存在100倍缩放问题? 怎么 ...
- 闪电侠第四季/全集The Flash迅雷下载
闪电侠第四季>(The Flash Season 4)是DC娱乐和华纳联手CW电视台制作的真人超级英雄系列剧,是美剧<闪电侠>系列的第四季. 该季于2017年10月10日在美国CW电 ...
- Android加密解密
随笔分类 - Android加密解密 Android数据加密之异或加密算法 摘要: 前言: 这几天被公司临时拉到去做Android IM即时通信协议实现,大致看了下他们定的协议,由于之前没有参与,据说 ...