Careercup - Google面试题 - 5205167846719488
2014-05-03 23:35
原题:
For a given node in binary search tree find a next largest number in search tree.
题目:给定一个二叉搜索树的节点,找出此节点在树中的中序后继节点,也就是比它大的最小节点。
解法:右子树向左走到底,左祖先走到顶向右。草稿纸上一画图就清楚了。
代码:
// http://www.careercup.com/question?id=5205167846719488
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
}; class Solution {
public:
int nextLargestNumber(TreeNode *root, int val) {
if (root == nullptr) {
// nothing to do
return val;
} left_top = nullptr;
return findRecursive(root, val);
}
private:
TreeNode *left_top; int findRecursive(TreeNode *root, int val) {
if (root == nullptr) {
// not found, return val
return val;
} else if (root->val > val) {
left_top = root;
return findRecursive(root->left, val);
} else if (root->val < val) {
return findRecursive(root->right, val);
} else {
if (left_top == nullptr) {
// val is the greatest of all.
return val;
}
if (root->right == nullptr) {
return left_top->val;
} left_top = root->right;
while (left_top->left != nullptr) {
left_top = left_top->left;
}
return left_top->val;
}
};
}
Careercup - Google面试题 - 5205167846719488的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- javaShop的一些总结
主要参考 pdf 找到对应的文件吧,具体怎么制作一个挂件 还没有理解里面的思路,就没有研究了,改一个商城项目遇到了,也只有慢慢解决 加油! CSDN下载地址:http://download.csdn. ...
- Ubuntu 15.04 无损扩展分区(目录)容量的方法 (无需格式化, 文件不丢失)
源 起 用了一段时间Ubuntu,碰到了UBuntu磁盘空间不足的问题, 最初我只给Ubuntu分配了30个G的空间, 昨天试用了一下VirtualBox安装了一个xp虚拟系统,用以解决Ubuntu下 ...
- AndroidTestCase简单使用
1.根据需求创建TestCase类,实现测试用例.此类需继承AndroidTestCase类 public class TestCase extends AndroidTestCase { @Over ...
- FusionCharts 相关知识
FusionCharts1.平均线: <trendLines><line startValue='{0}' toolText='平均线' color='#FF0000' displa ...
- raphael画图
// 在坐标(10,50)创建宽320,高200的画布 var paper = Raphael(10, 50, 320, 200); // 在坐标(x = 50, y = 40)绘制半径为 10 的圆 ...
- [Android开发系列]IT博客应用V1.3
首先,感谢使用这款软件并给我意见的朋友们,有你们的意见,才有了这个版本. 其次,检索功能和分类筛选功能(如果是你提的意见,记得在下面mark哦,毕竟读代码你能发现,其实发意见这个就是用自己的邮箱给自己 ...
- iOS - SWift3 & XCode8
1. 使用资源文件夹导入并管理图片素材 /* *资源文件夹可以方便您进行图片管理,在读取图片时,不需要加上图片名的后缀.同时还可以提高软件的安全性,它会讲图片都加密压缩, *并保存到 Assets ...
- 郑州轻工业OJ1400--这不可能是情书吧
地址:http://acm.zzuli.edu.cn/problem.php?id=1400 #include<stdio.h> #include<string.h> #inc ...
- 杭电ACM2076--夹角有多大(题目已修改,注意读题)
杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...
- EasyUI_Datagrid学习总结
EasyUI_Datagrid学习总结 2016年7月25日星期一 一.简介 Easyui中的datagrid从总的作用上讲,就是在列表上显示数据,类似于table,但是在table的基础上,此控件更 ...