[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 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- VSCode-python 进阶配置
VSCode-python 进阶配置 中文乱码 中文乱码,网上一堆解决方法,但是根本没有有效起作用的. 在python脚本的前面添加: # -*- coding:utf-8 -*- 并不能在控制台输出 ...
- 关于method not exist:think\db\Query->render错误解决方法
{volist name="Teachers" id="Teachers" key="key"} <tr> <td> ...
- HTML:一个form表单有两个按钮,分别提交到不同的页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 时间序列算法理论及python实现(1-算法理论部分)
如果你在寻找时间序列是什么?如何实现时间序列?那么请看这篇博客,将以通俗易懂的语言,全面的阐述时间序列及其python实现. 就餐饮企业而言,经常会碰到如下问题. 由于餐饮行业是胜场和销售同时进行的, ...
- Android(java)学习笔记17:网络编程的概述
1. 计算机网络 计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统 ...
- 【[HAOI2016]找相同字符】
其实这道题跟[AHOI2013]差异很像 其实这个问题的本质就是让你算所有后缀的\(lcp\)长度之和,但是得来自两个不同的字符串 先把两个字符串拼起来做一遍\(SA\),由于我们多算了来自于同一个串 ...
- 2018.11.17 Struts2框架入门
Struts2 框架学习 一.struts2是什么? (1)概念 (2)struts2使用优势 自动封装参数 参数校验 结果的处理(转发|重定向) 国际化 显示等待页面 表单的防止重复提交 (3)st ...
- 逻辑回归(分类问题)(Logistic Regression、罗杰斯特回归)
逻辑回归:问题只有两项,即{0, 1}.一般而言,回归问题是连续模型,不用在分类问题上,且噪声较大,但如果非要引入,那么采用逻辑回归模型. 对于一般训练集: 参数系统为: 逻辑回归模型为: ...
- java反射 反射构造函数 报 wrong number of arguments 错误
package com; import java.lang.reflect.Constructor; public class Person { public Person() { } public ...
- idea连接sqlite
首先下载驱动 官网链接:http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc 打开idea 第一步:右边 数据源 (如果没有显示单击这里,有 ...