[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 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- vue-cli3 项目从搭建优化到docker部署
项目地址 vue-cli3-project 欢迎 star 原文地址 https://www.ccode.live/lentoo/list/9?from=art 1. 创建一个vue项目 相信大部分人 ...
- pl/sql中的一种链接数据库方式
今天看开发人员如此连接数据库:
- JsonResponse、FileResponse和StreamingHttpResponse
一.JsonResponse对象 class JsonResponse(data,encoder=DjangoJSONEncoder,safe=True,json_dumps_params=None, ...
- Django运行SQL语句
1.Manager.raw(raw_query, params=None, translations=None) >>> for p in Person.objects.raw('S ...
- windows 网络通讯模型Overlapped (转)(未看)
https://blog.csdn.net/jofranks/article/details/7895316 https://blog.csdn.net/caoshiying/article/deta ...
- Android(java)学习笔记18:单例模式
单例模式代码示例: 1. 单例模式之饿汉式: package cn.itcast_03; public class Student { // 构造私有 private Student() { } // ...
- ACM-ICPC(11/8)
URAL 1005 给定一些石头的重量,要求分成两个部分最相近.二进制暴力枚举. #include <bits/stdc++.h> using namespace std; ]; int ...
- LA 4670 AC自动机
题意:给一个字典,看这个字典中匹配最多次数的是哪个单词(可以有多个). 分析: AC自动机就是用来解决多模式匹配问题的工具. 模板用的lrj的,相比HDU 2222,动态开辟字典树.用last数组统计 ...
- [19/03/23-星期六] 容器_ 泛型Generics
一.概念 生活中的容器不难理解,是用来容纳物体的,程序中的“容器”也有类似的功能,就是用来容纳和管理数据. 数组就是一种容器,可以在其中放置对象或基本类型数据. ---优势:是一种简单的线性序列,可以 ...
- 【luogu P1186 玛丽卡】 题解
题目链接:https://www.luogu.org/problemnew/show/P1186 邻接表开大开小真的奇妙,毒瘤玩意,再您妈的见. #include <queue> #inc ...