2014-05-02 07:18

题目链接

原题:

boolean isBST(const Node* node) {
// return true iff the tree with root 'node' is a binary search tree.
// 'node' is guaranteed to be a binary tree.
} n
/ \
a b
\
c

题目:检查一棵二叉树是否为二叉搜索树。

解法:二叉搜索树,就是每个节点的左边全都小于它,右边全都大于它。如果真的对于每个节点都全部检查左右边的每个节点,就做了很多重复劳动。只需要判断每个节点的左子树最靠右,和右子树最靠左的节点是否小于和大于它即可。递归过程中传递引用可以随时更新两个需要检查的值。请看代码。

代码:

 // http://www.careercup.com/question?id=5632735657852928
#include <climits>
#include <iostream>
#include <sstream>
#include <string>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int _val = ): val(_val), left(nullptr),right(nullptr) {};
}; class Solution {
public:
bool isBST(TreeNode *root) {
if (root == nullptr) {
return false;
} max_val = INT_MIN;
res = true;
first_node = true;
isBSTRecursive(root); return res;
};
private:
int max_val;
bool res;
bool first_node; void isBSTRecursive(TreeNode *root) {
if (!res) {
return;
} // root is guaranteed to be not nullptr.
if (root->left) {
isBSTRecursive(root->left);
}
if (first_node || root->val > max_val) {
first_node = false;
max_val = root->val;
} else {
res = false;
return;
}
if (root->right) {
isBSTRecursive(root->right);
}
};
}; void construcTree(TreeNode *&root)
{
int val;
stringstream sio;
string s; if (cin >> s && s != "#") {
sio << s;
sio >> val;
root = new TreeNode(val);
construcTree(root->left);
construcTree(root->right);
} else {
root = nullptr;
}
} void deleteTree(TreeNode *&root)
{
if (root == nullptr) {
return;
}
deleteTree(root->left);
deleteTree(root->right);
delete root;
root = nullptr;
} int main()
{
TreeNode *root;
Solution sol; while (true) {
construcTree(root);
if (root == nullptr) {
break;
} cout << (sol.isBST(root) ? "Valid BST" : "Invalid BST") << endl; deleteTree(root);
} return ;
}

Careercup - Facebook面试题 - 5188884744896512的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. C#_字符串的操作

    1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ...

  2. 我的博客模板(线框图wireframe)

    不久前看到一篇介绍定制网页浏览的方法,当时就想着,我把我的博客页也修改下,在手机浏览的时候,也能漂亮的显示出来.以后写的文章的话,也可以分享的微信朋友圈里面和朋友分享. 具体步骤参考:http://w ...

  3. 如何用C#代码查找某个路径下是否包含某个文件

      string path = "f:\\哈哈"; if (Directory.Exists(path)) { MessageBox.Show("存在文件夹") ...

  4. php数组中删除元素之重新索引

    如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊 <?php $arr = array('a','b','c','d'); unset($arr[1]); ...

  5. iOS - OC & Xcode

    一.入门 1.1 iOS模版介绍 1.2 简单工程项目 1.3 设置App启动的设备方向 1.4 Xcode界面介绍 1.5 快速查找文件 1.6 快速更改同名变量 1.7 将代码提取为方法 1.8 ...

  6. IOS开发: 为UIImageView添加点击事件

    转载于:http://www.pocketdigi.com/20140218/1276.html UIImageView并不像UIButton一样,点点鼠标就可以关联点击事件,也不像Android里有 ...

  7. AMQ学习笔记 - 13. Spring-jms的配置

    概述 如何使用spring-jms来简化jms客户端的开发? 这篇文章主要记录如何配置以便以后复用,而非原理的讲解,有些内容我 没有掌握原理. producer端 producer端负责发送,这里使用 ...

  8. hdu 2176 取(m)石子游戏

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 题意分析:给出M堆石子,两人交替取子,给出先手能否胜利. 不能输出No, 能则输出Yes并给出第 ...

  9. 2016.10.30 济南学习 Day2 下午 T1

    他 [问题描述] 一张长度为N的纸带,我们可以从左至右编号为0 − N(纸带最左端标号为 0).现在有M次操作,每次将纸带沿着某个位置进行折叠,问所有操作之后纸带 的长度是多少. [输入格式] 第一行 ...

  10. Mongodb Java Driver 参数配置解析

    要正确使用Mongodb Java Driver,MongoClientOptions参数配置对数据库访问的并发性能影响极大. connectionsPerHost:与目标数据库能够建立的最大conn ...