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. Windows Host 文件

    Windows XP Home / Windows 7/ Windows Server 2008 c:\windows\system32\drivers\etc\hosts 如果碰到Localhost ...

  2. *1 Two Sum two pointers(hashmap one scan)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. CSS:响应式下的折叠菜单(条纹式)

    原文:CSS: Responsive Navigation Menu 译文:CSS:响应式导航菜单 译者:dwqs 写在之前,关于如何制作响应式的下拉菜单:响应式下的下拉菜单 之前,我写了一篇关于怎么 ...

  4. 【HHHOJ】ZJOI2019模拟赛(十三)03.10 解题报告

    点此进入比赛 得分: \(97+0+10=107\) 排名: \(Rank\ 3\) \(Rating\):\(+47\) \(T1\):[HHHOJ187]Hashit(点此看题面) 容易想到可以用 ...

  5. create-react-app项目使用假数据

    做新项目的时候,前端每次要等后端接口准备好再开始,就会延期,等后端接口准备好了,前端这边的项目又会相互紧张,如果前端跟后端同时进行,前期将框架,基础做好,定好接口文档,前端在后端没准备好接口的时候使用 ...

  6. jQuery 效率提升建议

    jQuery简洁通用的方法集把编码者从繁重的工作中解脱出来,也拉低了进入javascript的门槛,初学者对浏览器兼容性一无所知的情况下,几行代码就可以写出超炫的特效.网上有一篇文章转载比较泛滥,已经 ...

  7. 【转】Android开发学习笔记(一)——初识Android

    对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...

  8. 剑指offer49 把字符串转换成整数

    这个代码会报错 class Solution { public: ,kinvalid}; int now_status = kvalid; int StrToInt(string str) { now ...

  9. jquery-ui-custom autocomplete

    //jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8& ...

  10. vue中登录模块的插件封装

    一个电商城的项目,场景是:在未登录的情况下点击收藏或者加入购物车等操作,执行一个方法如this.$login()来动态插入登录组件. 第一步:写好关于这个登录弹窗的单文件组件 loginBox.vue ...