[Cracking the Coding Interview] 4.3 List of Depths
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的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- linux中启动网卡报错:Bringing up interface eth1: Error: Connection activation failed
在重启linux网络服务的时候提示: Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/2 并且产生报错 ...
- 【java开发系列】—— 深克隆和浅克隆
Java支持我们对一个对象进行克隆,通常用在装饰模式和原型模式中.那么什么是深克隆,什么是浅克隆呢. [浅克隆],通常只是对克隆的实例进行复制,但里面的其他子对象,都是共用的. [深克隆],克隆的时候 ...
- 抓取android系统日志_记录一次定位app闪退故障
在测试android客户端兼容性时,发现app闪退,上海的小伙伴需要闪退时的系统日志:故把快生锈的adb知识拿出来show一把: 1.下载adb工具包(adb的全称为Android Debug Bri ...
- chrome不能用百度网盘极速上传插件的解决办法
进入chorme设置中,选择隐私设置中的内容设置,插件>自动运行,然后管理例外情况>添加“[*.]baidu.com”,后边当然选择允许,然后重启浏览器,OK了
- SPOJ8093【JZPGYZ - Sevenk Love Oimaster】
怎么全是广义后缀自动机,我\(AC\)自动机不服 这道题可以使用的算法很多,\(SA\)或者\(SAM\)应该都可以 但是我都不会 但是这毕竟是一个多串匹配问题,\(AC\)自动机还是可以刚一刚的 我 ...
- Nmap的基础知识
扫描单一的一个主机: #nmap domain.com #nmap 192.168.1.2 扫描整个子网: #nmap 扫描多个目标: #nmap 192.168.1.2 192.168.1.5 查看 ...
- Python的基本库与第三方库
一:Python 模块,包,库的概念理解: 1.python模块是: python模块:包含并且有组织的代码片段为模块. 表现形式为:写的代码保存为文件.这个文件就是一个模块.sample.py 其中 ...
- 2018.11.7 关于将Web项目部署到阿里云服务器-5个步骤搞定
将Eclipse导出的War包部署到阿里云服务器上,提供给移动端实时的访问 1. 先登录阿里云网站注册账号,选择服务器类型(我用的是 云服务器ECS), 如果你还是在读大学生可享受优惠价,最低好像是9 ...
- 不能安装这个“安装 OS X EI Capitan”应用程序副本不能验证” 的解决办法
不能安装这个“安装 OS X EI Capitan”应用程序副本不能验证.它在下载过程中可能已遭破坏或篡改. 解决办法:修改系统时间. 1,查看你的dmg文件的创建日期 2,把你的系统日期修改到比这个 ...
- js控件设置只读属性和不可用属性
介绍js实现只读的几种方法: 设置控件只读的话,我们要先清楚哪些没有readOlny这个属性 我知道的checkbox和RadioButton没有readOlny这个属性,我上一篇已经介绍了如何设置c ...