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 ...
随机推荐
- 每日一词【命令行CMD】
CURL 中文:命令行URL下载 英文解释:CommendLine Uniform Resource Locator 使用场景: 文件传输 curl是利用URL语法在命令行方式下工作的开源文件传输工具 ...
- CSS之侧边栏
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- sql server 锁学习
insert 默认加的锁是 不允许select,update 但是可以insert update 默认加的锁是 不允许 update 可以 select ,insert
- WampServer修改Mysql密码的步骤
1.安装成功后,通过 phpmyadmin 进入mysql,点击上面的 [用户] 菜单,在用户[root]主机[localhost]点击编辑权限,下面有一个选项[修改密码],输入您想要的密码,如:12 ...
- html5的自定义data-*属性和jquery的data()方法的使用示例
人们总喜欢往HTML标签上添加自定义属性来存储和操作数据. 但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它 ...
- Objective-C 【电商APP应用代码-系统分析-详细注释-代码实现】
------------------------------------------- 电商APP应用 ************************************************ ...
- VS2008简体中文正式版序列号
VS2008简体中文正式版序列号 1.Visual Studio 2008 Professional Edition:XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4WT 2.Visual St ...
- 20150506—WinForm自动生成按钮&按钮拖动
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C++四种不同的对象生存方式
在C++中,有四种方法产生一个对象. 第一种方法是在堆栈(stack)之中产生: void MyFunc() { CFoo foo;//在堆栈(stack)中产生foo对象 } 第二种方法是在堆(he ...
- java 自动装箱和自动拆箱
自动装箱 java执行Integer i = 100:编译器编译成Integer i = Integer.valueOf(100); Integer i = 100; //编译器编译成Integer ...