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. 宽度的100%和auto的区别

    前段时间做项目,发现分不清width设为100%和auto的区别,实在是太水了,就查了点资料,做个总结,有不对的地方欢迎大家指出. width:auto 块级元素默认的宽度值.看一下MDN上的解释:T ...

  2. Java之累加和

    所谓累加算法,就是数学中数列求的算法,这都是司空见惯了的.下面我们用java求: package com.cdp.leijiahe; import java.util.Scanner; public ...

  3. NSString字符操作

    1.常用创建初始化方法 1.NSString *string0 = @"string"; 2.NSString *string1 = [NSString stringWithFor ...

  4. 20150511---Timer计时器(备忘)

    private void timer1_Tick(object sender, EventArgs e) { TimeSpan ts = , , ); string str = ts.Hours + ...

  5. Tomcat7出现HTTP Status 500 - java.lang.ClassCastException: org.apache.jasper.el.ELContextImpl cannot be cast to org.apache.jasper.el.ELContextImpl

    今天在Tomcat7上发布了一个war,过一阵子发现localhost:8080都进不去了.在浏览器输入http://localhost:8080出现如下内容:

  6. [Bootstrap]组件(一)

    Glyphicons字体图标 基类.glyphicon  {position/top/display/font-family/} 具体类  {content}  --表现在伪元素上 使用要点:a.基类 ...

  7. DBCP之----"数据库"与"连接池"的连接建立过程

    1 public class DBCPTest { 2 /* 3 * 使用BasicDataSource类,通过url, 4 和diverClass,username,password, 5 几个参数 ...

  8. Spring AOP整理

    示例展示 AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充.AOP之所以能得到广泛认可,主要是因为它将应用系统拆分分了 ...

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

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

  10. 《Linux系统free命令的使用》学习笔记

    free命令用于显示当前系统的内存空闲和使用情况,其中包括物理内存,交换分区内存,内核缓冲区内存以及高速缓存,free的参数报错一下: -b ——字节的方式显示内存使用情况 [root@redhat ...