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. PowerDesigner的数据类型

    用PowerDesigner 15 设计个数据库,每个字段的数据类型设计真是头大,根据字段意思看用哪个类型最合适还得仔细研究呀.贴几个数据类型表格收藏一下^_^ Numeric data types ...

  2. 用viewpager实现图片轮播

    应用中常常遇到图片轮播的需求,这时候就需要用到viewpager这个组件.viewpager是android support v4 中提供的一个组件.viewpager使用需要以下几步骤: 1.在布局 ...

  3. python基础day2作业:购物车

    #功能:1.可注册账号2.登录买家账号3.可查询编辑购物车里商品4.可以余额充值5.可提示余额不足6.购物车结算 #使用:1.第一次使用先注册账号填写账号金额2.账号金额信息保存在buyer_acco ...

  4. Mysql 格式化日期格式

    DATE_FORMAT(date, format) 根据格式串format 格式化日期或日期和时间值date,返回结果串. 可用DATE_FORMAT( ) 来格式化DATE 或DATETIME 值, ...

  5. 原生js实现addClass,removeClass,hasClass方法

    function hasClass(elem, cls) { cls = cls || ''; if (cls.replace(/\s/g, '').length == 0) return false ...

  6. 8款超酷而实用的CSS3按钮动画

    1.CSS3分享按钮动画特效 这是一款基于CSS3的社会化分享按钮,按钮非常简单,提供了分享到twitter.facebook.youtube等大型社交网站.每一个分享按钮都有个大社交网站的Logo图 ...

  7. ABAP OO与ALV结合方式探索(2)

    接上篇 一开始设计的BO 类是为了实现功能而实现功能 从类的单一职责的角度而言 先把这个BO对象拆分   这里又有一个需要考虑的点:   如何传递内表数据到ALV 如果引入一个中间变量,数据就会被do ...

  8. UIPickerView基本用法

    #import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  9. js 求前n项的 fibnaci 数列和

    function f(n) { var num1 = 1, num2 = 1; if (n == 1) document.write(num1);//n=1,输出1 else if (n > 1 ...

  10. mycat1.5~1.6的一个bug

    以下语句在mysql单库中执行正常: SELECT * FROM device WHERE devicetype='AMS.Monitoring.XlCloud.QKL8154.XLCloudDevi ...