《Cracking the Coding Interview》——第4章:树和图——题目1
2014-03-19 03:30
题目:判断一个二叉树是否为平衡二叉树,即左右子树高度相差不超过1。
解法:递归算高度并判断即可。
代码:
// 4.1 Implement an algorithm to check if a bianry tree is height-balanced.
#include <algorithm>
#include <cstdio>
#include <unordered_map>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right; TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
}; void constructBinaryTree(TreeNode *&root)
{
int val; scanf("%d", &val);
if (val <= ) {
root = nullptr;
} else {
root = new TreeNode(val); constructBinaryTree(root->left);
constructBinaryTree(root->right);
}
} void clearBinaryTree(TreeNode *&root) {
if (root == nullptr) {
return;
} else {
clearBinaryTree(root->left);
clearBinaryTree(root->right);
delete root;
root = nullptr;
}
} void calcHeights(TreeNode *root, unordered_map<TreeNode *, int> &heights)
{
if (root == nullptr) {
heights[root] = ;
} else {
calcHeights(root->left, heights);
calcHeights(root->right, heights);
heights[root] = max(heights[root->left], heights[root->right]) + ;
}
} bool isBalanced(TreeNode *root, unordered_map<TreeNode *, int> &heights)
{
if (root == nullptr) {
return true;
} else {
return abs(heights[root->left] - heights[root->right]) <= ;
}
} int main()
{
TreeNode *root;
unordered_map<TreeNode *, int> heights; while (true) {
constructBinaryTree(root);
if (root == nullptr) {
break;
} calcHeights(root, heights);
if (isBalanced(root, heights)) {
printf("Yes\n");
} else {
printf("No\n");
}
heights.clear();
clearBinaryTree(root);
} return ;
}
《Cracking the Coding Interview》——第4章:树和图——题目1的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 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(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 《Cracking the Coding Interview》——第4章:树和图——题目9
2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...
- 《Cracking the Coding Interview》——第4章:树和图——题目8
2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...
随机推荐
- 1.10 从表中随机返回n条记录
同时使用内置函数的rand函数. limit 和order by: select * from emp order by rand() limit 2;
- HashMap扩容
前言:当您在读该文章的时候,我认为您已经知道HashMap的底层实现原理,如果您还不清楚HashMap是如何实现的,请先去了解,再回来看本文章. 1.HashMap什么时候扩容? HashMap的容量 ...
- 基于稀疏表示的图像超分辨率《Image Super-Resolution Via Sparse Representation》
由于最近正在做图像超分辨重建方面的研究,有幸看到了杨建超老师和马毅老师等大牛于2010年发表的一篇关于图像超分辨率的经典论文<ImageSuper-Resolution Via Sparse R ...
- python遇到IndentationError: unexpected indent
由于tab和空格混用而导致的问题,解决办法如下: 在SubLime中View中的Identation中的Convert Indentitation to Spaces即可
- IIS配置MIME类型
有时候我们上传的视频,如果IIS上没有配置此格式是播放不了的.这个时候需要你在IIS上添加这个类型才能播放. MIME类型 ①打开你的IIS,点你的网站 ②双击 MIME类型 ③右键-->添加 ...
- wgan pytorch,pyvision, py-faster-rcnn等的安装使用
因为最近在读gan的相关工作,wgan的工作不得不赞.于是直接去跑了一下wgan的代码. 原作者的wgan是在lsun上测试的,而且是基于pytorch和pyvision的,于是要装,但是由于我们一直 ...
- python的**和*
1.**两个乘号就是乘方,比如2**4,结果就是2的4次方,结果是16一个乘号*,如果操作数是两个数字,就是这两个数字相乘,如2*4,结果为8*如果是字符串.列表.元组与一个整数N相乘,返回一个其所有 ...
- JAVAWEB开发中过滤器的概述及使用
1.什么是过滤器? 过滤器是向WEB应用程序的请求和响应添加功能的WEB服务组件 2.过滤器的作用 1)可以统一的集中处理请求和响应 2)可以实现对请求数据的过滤 3.过滤器的工作方式 4.使用场合 ...
- jQuery、Angluar、Avalon对比
最近在慕课网看一些关于avalon的视频,记录下一些笔记及代码实例以便日后自己复习可以用到,另外也可以给不想花时间看视频的小伙伴提供一丝丝帮助 这里主要是做一个简单的todolist 分别用三种不同的 ...
- 泉五培训Day2
T1 旅游 题目 [题目描述] 幻想乡有n个景点(从1开始标号),有m条双向的道路连在景点之间,每条道路有一个人气值d,表示这条道路的拥挤程度.小G不会经过那些人气值大于x的道路,她想知道有多少对景点 ...