Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth.(e.g., if you have a tree with depth D, you'll have D linked lists)

这道题给定一个二叉树,要求我们对于每一层的节点建立链表。这里涉及几个数的概念,depth, height, level. 你能分得清楚吗?看以下的定义。

Height of node – The height of a node is the number of edges on the longest downward path between that node and a leaf.

  • 从node到leaf会有多条路径,最长的路径的边的条数是这个node的height。

Height of tree - The height of a tree is the number of edges on the longest downward path between the root and a leaf.

  • 所以有 the height of a tree is the height of its root.
  • 我们经常会问这样的问题:what is the max number of nodes a tree can have if the height of the tree is h?Answer: 2^h - 1

Depth –The depth of a node is the number of edges from the node to the tree's root node.

  • Depth 和 Height比是方向相反的,Height是以leaf为基准,而Depth以root为基准
  • depth of root is 0.

Level – The level of a node is defined by 1 + the number of connections between the node and the root.

  • level = depth + 1
  • 要注意level从1开始,depth从0开始!!

这道题要求我们对tree的每一个depth的nodes建立个链表,实际上就是对数进行层序遍历,BFS是经典的层序遍历的方法,见如下代码:

class TreeNode
attr_accessor :val, :left, :right def initialize(val)
@val = val
@left = nil
@right = nil
end
end class LinkedListNode
attr_accessor :val, :next def initialize(val)
@val = val
@next = nil
end
end def build_linked_lists(root)
return [] if root.nil? lists = []
q = Queue.new
q << root while !q.empty?
size = q.size dummy = LinkedListNode.new(0)
head = dummy size.times do
front = q.pop
head.next = LinkedListNode.new(front.val)
head = head.next q << front.left if front.left
q << front.right if front.right
end
lists << dummy.next
end
lists
end

[Cracking the Coding Interview] 4.3 List of Depths的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

随机推荐

  1. Eclipse集成Maven(手工安装Maven且手工集成到Eclipse)

    1.操作环境 操作系统:win8 64位 IDE:Helios Eclipse 1.8 JDK:1.6 2.资源准备 2.1 maven安装包: apache-maven-3.2.5-bin.zip  ...

  2. vue-cli3 项目从搭建优化到docker部署

    项目地址 vue-cli3-project 欢迎 star 原文地址 https://www.ccode.live/lentoo/list/9?from=art 1. 创建一个vue项目 相信大部分人 ...

  3. 1.Zabbix 3.0 基础

    请查看我的有道云笔记: http://note.youdao.com/noteshare?id=85046af7675851675679a47beadc7aa3&sub=000AB0B2409 ...

  4. CRM product UI里assignment block的显示隐藏逻辑

    我们在product overview page里能看到很多的assignment block.但是runtime的时候真正显示出来的内容只是configuration里面看到内容的一个子集. Pro ...

  5. OAuth 2.0协议在SAP产品中的应用

    阮一峰老师曾经在他的博文理解OAuth 2.0里对这个概念有了深入浅出的阐述. http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html 本文会结合我 ...

  6. react中性能优化的点

    react提升代码性能的点 1.绑定如果改变作用域点话放在constructor里面做,这样可以保证整个程序的作用域绑定操作只会执行一次,而且避免子组件的无谓渲染. 2.内置的setState是个异步 ...

  7. emmet工具使用和技巧

    介绍 在前端开发的过程中,一大部分的工作是写 HTML.CSS 代码.特别是手动编写 HTML 代码的时候,效率会特别低下,因为需要敲打很多尖括号,而且很多标签都需要闭合标签等.于是,就有了 Emme ...

  8. 【luogu P2939 [USACO09FEB]改造路Revamping Trails】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2939 本来说是双倍经验题,跟飞行路线一样的,结果我飞行路线拿deque优化SPFA过了这里过不了了. 所以多 ...

  9. SpringMvc获取上下文

    import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.spri ...

  10. Redis工具之Jedis

    //jedis的连接池 public void test1(){ //创建连接池配置对象 JedisPoolConfig poolConfig = new JedisPoolConfig(); poo ...