《Cracking the Coding Interview》——第4章:树和图——题目5
2014-03-19 04:11
题目:设计算法检查一棵二叉树是否为二叉搜索树。
解法:既然是二叉搜索树,也就是说左子树所有节点都小于根,右子树所有节点都大于根。如果你真的全都检查的话,那就做了很多重复工作。只需要将左边最靠右,和右边最靠左的节点和根进行比较,然后依照这个规则递归求解即可。
代码:
// 4.5 Check if a binary tree is binary search tree.
#include <cstdio>
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);
}
} bool postorderTraversal(TreeNode *root, TreeNode *&left_most, TreeNode *&right_most)
{
TreeNode *ll, *lr, *rl, *rr;
bool res_left = true, res_right = true; if (root->left != nullptr) {
if (!postorderTraversal(root->left, ll, lr)) {
return false;
}
if (lr->val >= root->val) {
// all left nodes must be smaller than the root.
return false;
}
} else {
ll = lr = root;
} if (root->right != nullptr) {
if (!postorderTraversal(root->right, rl, rr)) {
return false;
}
if (rl->val <= root->val) {
// all right nodes must be greater than the root.
return false;
}
} else {
rl = rr = root;
}
left_most = ll;
right_most = rr; return true;
} void clearBinaryTree(TreeNode *&root) {
if (root == nullptr) {
return;
} else {
clearBinaryTree(root->left);
clearBinaryTree(root->right);
delete root;
root = nullptr;
}
} int main()
{
TreeNode *root;
TreeNode *left_most, *right_most; while (true) {
constructBinaryTree(root);
if (root == nullptr) {
break;
} left_most = right_most = nullptr;
if (postorderTraversal(root, left_most, right_most)) {
printf("Yes\n");
} else {
printf("No\n");
} clearBinaryTree(root);
} return ;
}
《Cracking the Coding Interview》——第4章:树和图——题目5的更多相关文章
- 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一模一样. 解法:首先可以根据 ...
随机推荐
- MySQL入门很简单: 9 插入 更新与删除数据
1. 插入数据:INSERT 1)为表的所有字段插入数据 第一种: 不指定具体的字段名 INSERT INTO 表名 VALUES(值1,值2,...,值n): 第二种:INSERT语句中列出所有字段 ...
- 二叉搜索树实现MAP
二叉搜索树的基本实现. /* Date: 2014-04-29 purpose: An implementation of MAP using binary search tree. */ #ifnd ...
- IOS SQLite函数总结
SQL语句的种类 ● 数据定义语句(DDL:Data Definition Language) ● 包括create和drop等操作 ● 在数据库中创建新表或删除表(create table或 ...
- IOS 分页(pagingEnabled)
self.scrollView.pagingEnabled = YES; - (void)nextImage { // 1.增加pageControl的页码 ; ) { page = ; } else ...
- 最长上升子序列&&最长不下降子序列
百练2757: 题目描述: 对于给定的序列,求出最长上升子序列的长度. 题目链接:http://bailian.openjudge.cn/practice/2757 解题思路 一.动态规划 1. 找子 ...
- 转:postMan 使用教程
转:https://www.cnblogs.com/alanjl/p/5490922.html 自从开始做API开发之后,我就在寻找合适的API测试工具.一开始不是很想用Chrome扩展,用的 Wiz ...
- Jmeter启动错误
错误一 1 apache-jmeter-2.13\bin>jmeter 'findstr' 不是内部或外部命令,也不是可运行的程序 或批处理文件. Not able to find Java e ...
- Next K Permutation
3457: Next K Permutation 时间限制: 1 Sec 内存限制: 128 MB提交: 4 解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 n 个数有 ...
- Maven项目导出可执行jar
配置文件中添加插件 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>m ...
- 简单使用mybatis(idea中使用)
首先创建一个maven项目 第一步:在pom.xml中添加依赖 <dependencies> <!--mybatis--> <dependency> <gro ...