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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. 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 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  9. 《Cracking the Coding Interview》——第4章:树和图——题目9

    2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...

  10. 《Cracking the Coding Interview》——第4章:树和图——题目8

    2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...

随机推荐

  1. 双网卡(一外一内)都启用,将内网卡默认网关去除即可正常连接Internet

  2. pat甲级1107

    1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...

  3. Graylog安装操作

    Graylog安装操作 实验环境centos7.5系统  mem:4-8G       disk:50G 关闭selinux以及firewalld 一.准备环境 1.1.java环境 下载java的j ...

  4. mysql的慢查询实战+sql优化

    背景:使用A电脑安装mysql,B电脑通过xshell方式连接,数据内容我都已经创建好,现在我已正常的进入到mysql中 步骤1:设置慢查询日志的超时时间,先查看日志存放路径查询慢日志的地址,因为有慢 ...

  5. G711格式语音采集/编码/转码/解码/播放

    2019-05-01 语音g711格式和AMR格式类似,应用很简单,很多人已经整理过了,收录于此,以备不时之需,用别人现成的足矣,我们的时间应该用来干更有意义的事. 1.PCM to G711 Fas ...

  6. 【转】NodeJS教程--基于ExpressJS框架的文件上传

    本文是翻译的一篇文章,原文地址:Handle File Uploads in Express (Node.js). 在NodeJS发展早期上传文件是一个较难操作的功能,随后出现了formidable. ...

  7. P1024 一元三次方程求解

    P1024 一元三次方程求解 #include<cstdio> #include<iostream> #include<algorithm> using names ...

  8. rbg的代码

    不得不赞rbg的代码,写的是真的好,各种异常都考虑到了,至少常见的异常没有了. 还有selective search的代码,也是很赞. 而edgebox的代码则不行啊,demo写的太死,而且代码里只能 ...

  9. 线段树的应用xx中学模拟lites

    跟昨天那个自己写的,没有按照模板来的一看风格就不相类似,今天模拟赛的时候就是用的我的那个自己YY的代码,才拿了10分.个人认为关键的问题应该在于对于数据的处理太过繁琐了,所以回来之后,就拿了大佬的程序 ...

  10. 【赛时总结】 ◇赛时·IV◇ CODE FESTIVAL 2017 Final

    ◇赛时-IV◇ CODE FESTIVAL 2017 Final □唠叨□ ①--浓浓的 Festival 气氛 ②看到这个比赛比较特别,我就看了一看--看到粉粉的界面突然开心,所以就做了一下 `(* ...