[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,循 ...
随机推荐
- One-wire Demo on the STM32F4 Discovery Board
One-wire Demo on the STM32F4 Discovery Board Some of the devs at work were struggling to get their s ...
- python实例[判断操作系统类型]
参考文献:http://bbs.chinaunix.net/thread-1848086-1-1.html 经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台( ...
- [Deepin 15] sudo source /etc/profile 提示找不到 source 命令(切换到 root 用户:sudo su)
在 Deepin/Ubuntu 系统 中,因为修改了下 配置文件,然后执行 source 命令重新加载配置文件,结果: sudo source /etc/profile 提示找不到 source 命令 ...
- JBPM使用方法、过程记录
一.How to call Web Service Using JBPM 5, designer https://204.12.228.236/browse.php?u=ObFK10b3HDFCQUN ...
- IEnumerable是集合,IEnumerator是集合的迭代器
我们常用IEnumerable,却忽视IEnumerator.简单来说,IEnumerable是可以被循环遍历的集合,IEnumerator实施循环遍历. 接口分别是: public interfac ...
- [转]过XX游戏驱动保护的代码
这个是过TX游戏自我保护驱动的源代码.可以过qq堂.DNF.寻仙等QQ游戏. #include <ntddk.h>#include <windef.h>#include < ...
- quartz 2.0 与1.0功能对比
日常开发来说,相对于1.0版,2.0版在使用上有以下几点需要注意的变化 变化一 比1.0多引用了C5.dll C5.dll 一个C#和其他CLI语言的泛型集合类..Net2.0及以上才可以使用.简介地 ...
- 关于struts2种的action运行两次,或多次,或反复运行的bug
今天在做项目的时候发现一个bug,就是action会莫名其妙的运行两次.网上搜了非常多帖子,关于这个问题也得到了一些处理方法,可是没有我想要的,造成运行两次活多次的问题呢,有非常多种原因,我在这里仅仅 ...
- IP视频通信中的"丢包恢复技术”(LPR)
转自:http://blog.csdn.net/blade2001/article/details/9094709 在IP视频通话中,即使是在丢包率很小的情况下也会对使用效果造成较为明显的影响.正是由 ...
- left join 注意事项
相信对于熟悉SQL的人来说,LEFT JOIN非常简单,采用的时候也很多,但是有个问题还是需要注意一下.假如一个主表M有多个从表的话A B C …..的话,并且每个表都有筛选条件,那么把筛选条件放到哪 ...