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

  1. [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 ...

  2. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. 【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, ...

  4. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  5. 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 ...

  6. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  7. 【Binary Tree Level Order Traversal II 】cpp

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  8. lettcode-102:Binary Tree Level Order Traversal (Java)

    Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...

  9. Binary Tree Level Order Traversal II 解题思路

    思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...

随机推荐

  1. STM32F103ZET6 用定时器级联方式输出特定数目的PWM

    STM32F103ZET6 用定时器级联方式输出特定数目的PWM STM32F103ZET6里共有8个定时器,其中高级定时器有TIM1-TIM5.TIM8,共6个. 这里需要使用定时器的级联功能,ST ...

  2. ISO 7816-4: GET RESPONSE and ENVELOPE command

    http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_7_transmission_interindustry_command ...

  3. Weekly linux and ConferenceByYear(2002-now)

    https://lwn.net/Archives/ https://lwn.net/Archives/ConferenceByYear/

  4. leetcode——169 Majority Element(数组中出现次数过半的元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. Silverlight for Windows Phone Toolkit

    Silverlight Toolkit 是一套codeplex上的很好的代码库,它里面包含了很多常用的但是Silverlight本身并不包含的控件.文档等内容.现在Silverlight Toolki ...

  6. 初步理解JWT并实践使用

    原文:https://www.jianshu.com/p/2fdc20a42c41 JWT是一种用于双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准(RFC 7519 ...

  7. shapefile与字符集编码设置

    在 ArcGIS Desktop (ArcMap, ArcCatalog, and ArcToolbox) 中,有编码页转换功能(CODE PAGE CONVERSION),可以读写多种字符编码的 s ...

  8. WordPress基础:get_page_link获取页面地址

    函数:get_page_link(页面id编号) 作用:获取指定页面的链接地址 用法: $link = get_page_link(2); 输出为:xxx/?page_id=2 如在循环里则不用填写i ...

  9. python测试开发django-11.模型models详解

    前言 Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库 只需要在settings ...

  10. HTML5学习笔记:跨域

    在跨域安全性方面,有多个地方会有限制,主要是XMLHttpRequest对象的跨域限制和iFrame的跨域限制,下面我们分别来看一下. Ajax跨域(CORS) CORS是一个W3C标准,全称是&qu ...