Careercup - Facebook面试题 - 5188884744896512
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的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- C# WinForm 调用WebService
在Winform中对数据库进行操作缺乏安全性,因而可以使用Winform调用WebService来实现对数据库的各种操作. 在VS2010中,创建一个Web服务程序,第一:创建一个空的Web应用程序, ...
- View的setOnClickListener的添加方法
1)第一种,也是最长见的添加方法(一下都以Button为例) 1 Button btn = (Button) findViewById(R.id.myButton);2 btn .setOnClick ...
- 慎用memset();
<span style="font-family: Arial, Helvetica, sans-serif;">void *(memset) (void *s,int ...
- Part 14 Mathematical functions in sql server
Part 29 Mathematical functions in sql server
- ueditor 单独图片上传 转载
<body> <script type="text/javascript"> //这个是图片上传的,网上还有附件上传的 (function($) { var ...
- chromium浏览器开发系列第二篇:如何编译最新chromium源码
说一下为什么这么晚才发第二篇,上周和这周department的工作太多了,晚上都是十点半从公司出发,回家以后实在没有多余的精力去摸键盘了.所以请大家包涵! 上期回顾: chromium源码下载: 1. ...
- WAS缓存导致修改文件不生效问题
当用ftp上传部署增量文件,was应用不生效问题解决方法:一. 修改web.xml文件,需要修改以下三个目录下的文件:1. /opt/IBM/WebSphere/AppServer/profiles/ ...
- websphere中由于实际应用没有卸载干净,导致安装不了。以下是完全卸载应用程序的方法
出现此问题的原因之一:操作界面上没有卸载完成.进行一下操作:1.删除 $WAS_HOME/profiles/AppSrv01/config/cells/...cell/applications下对应 ...
- [译]开始学习webpack
写在前面: 文章翻译自petehunt大神的petehunt/webpack-howto,作为学习webpack的开始.fork了一份,翻译后的在这里https://github.com/zjzhom ...
- C++ 11 之推导关键词
C++ 11新增了两个推导关键词,auto & decltype 1.区别 auto:用于推导变量类型: decltype: 用于推导表达式或者函数返回值 2.直接上代码 intmain() ...