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的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. 四步完成ajax的使用

    什么是ajax? ajax(异步javascript xml) 能够刷新局部网页数据而不是重新加载整个网页. 如何使用ajax? 第一步,创建xmlhttprequest对象,var xmlhttp ...

  2. ASP.NET缓存 Cache之数据缓存

    添加 Cache[Key]=object  or Cache.Insert 移除 Cache.Remove(key) 1.将值直接写入Cache 代码如下 复制代码 HttpContext.Curre ...

  3. javascript将浮点数转换成整数的三个方法

    浮点数转换成整数方法有很多,本例为大家介绍常用的三个方法,如果读者想到其他好用方法,也可以交流一下   Summary 暂时我就想到3个方法而已.如果读者想到其他好用方法,也可以交流一下 parseI ...

  4. 理解C#系列 / 核心C# / 判断&循环&跳转

    判断&循环&跳转 说明 本节写的是C#语言的控制程序流的语句,“控制程序流”就是控制程序运行流程的意思. 判断 很容易理解:如果……就…… if语句:测试特定条件是否满足,如果满足就执 ...

  5. (转)IIS7 优化-网站请发并发数

    1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings Queu ...

  6. 升级ionic版本后,创建新项目报Error Initializing app错误解决

    命令行,进入项目路径后,运行 ionic start myApp --v2 命令执行后,报如下错误 Installing npm packages...Error with start undefin ...

  7. GVIM:在WINDOWS下清爽写代码

    上大学后,你是不是也开始学习C语言了?特别是计算机学院的孩子,应当有更高的追求.C语言开课一段时间了,你是不是开始嫌弃IDE恶心的界面了?是不是跟我一样,嫌弃IDE打开速度太慢?VS2010需要12秒 ...

  8. cocos2d-x 节点操作 -->J2ME

      cocos2d-x 的节点操作涉及到以下几点          1.  节点之间的关系          2.  节点的添加操作          3.  节点的删除操作          4.  ...

  9. 手机网页制作的认识(有关meta标签)

    近日以来一直在看JQuery Mobile 一个手机开发框架,说实话真的很头疼的~~~~ 因为里面有很多的属性.方法和事件~~~ 下面是手机网页的一些认识: 一.<meta name=" ...

  10. 转:关于Apache与Nginx的优势比较(经典)

    不断有人跟我说Nginx比Apache好.比Apache快之类.Nginx更主要是作为反向代理,而非Web服务器使用.我翻译过一本关于反向代理的技术书籍,同时精通Apache API开发,对Nginx ...