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. Datazen 自定义地图--中国地图

    背景: 关于Datazen可以google一下,因为目前Datazen还没有中文版,所以google出来的资料会多一点,由于公司想用Datazen来做报表展示,所以有了下文. 参考文章: 中文---h ...

  2. Part 36 to 39 Talking about Delegates in c#

    Part 36 Delegates in c# Part 37 Delegates usage in c# class Progim { public static void Main() { Lis ...

  3. 用宏定义封装LoadLibrary,方便的动态加载dll

    同学们动态加载dll的时候是不是感觉挺麻烦的,每次都::LoadLibrary,::GetProcAddress,还要typedef一堆函数.最近闲来无聊,用宏封装了一下,可以少写不少代码,用来也挺方 ...

  4. 调用WCF接口的方法

    通过对接口调用可能出现的异常作出判断和处理,避免资源的浪费和占用~ public class SvcHelper { public static void Using(T client, Action ...

  5. 第一个Cocos2d-JS游戏

    我们的编写的第一个Cocos2d-JS程序,命名为HelloJS,从该工程开始学习其它的内容.创建工程我们创建Cocos2d-JS工程可以通过Cocos2d-x提供的命令工具cocos实现,但这种方式 ...

  6. 20150414---ListView简介(web)

    ListView,自带分页功能,而且用户自定义界面样式自由度高. 如下图,都是使用Listview完成的,(测试数据,内容较乱) 所在位置:工具--数据-ListView 这里是配置ListView样 ...

  7. UI2_IOS坐标系

    // // AppDelegate.m // UI2_IOS坐标系 // // Created by zhangxueming on 15/6/29. // Copyright (c) 2015年 z ...

  8. http: URL、请求方式

    URL:统一资源定位符,可以从互联网得到和访问资源,是标准资源的位置 结构包括协议.服务器名称(IP地址).端口(有的服务器需要).路径.文件名 协议:告诉浏览器如何处理打开的文件,常用的就是http ...

  9. mysql颠覆实战笔记(五)--商品系统设计(二):定时更新商品总点击量

    继续回到沈老师的MYSQL颠覆实战,首先回顾下上一节课的内容,请大家会看下上节课写的存储过程. 打开prod_clicklog表, 我们只要把日期(不含时分秒)的部分存在数据库中, 如果同一日期有相同 ...

  10. Android 技术用于汇总

    id 名词 含义 详细  1 Android CTS     CTS 全称 Compatibility Test Suite 兼容性测试工具 当产品开发出来以后,并定制了自己的 Android 系统后 ...