Write an algorithm to find the 'next' node(i.e. in-order successor) of a given node in a binary search tree.

这道题让我们寻找给定节点的中序后继节点,最简单的方法就是中序遍历tree, 并用一个prev指向当前的节点的前面的节点,如果prev等于等于给定节点,当前节点就是所求。见如下代码:

def inorder_successor(root, node)
$prev = nil
$succ = nil inorder(root, node)
$succ
end def inorder(root, p)
return if root.nil? inorder(root.left, p) if $prev == p
$succ = root
end $prev = root
inorder(root.right, p)
end

[Cracking the Coding Interview] 4.6 Successor 后继节点的更多相关文章

  1. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  2. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第4章:树和图——题目6

    2014-03-19 04:16 题目:找出一棵二叉搜索树中的中序遍历后继节点,每个节点都有指针指向其父节点. 解法1:分两种情况:向下走时,先右后左:向上走时,先左后右.如果目标节点有右子树,就向右 ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

随机推荐

  1. WAKE-WIN10-SOFT-CMAKE

    1,CMAKE 官网:https://cmake.org/ 下载:https://cmake.org/download/ BING:https://www.bing.com/search?q=cmak ...

  2. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  3. E: Unable to locate package

    E: Unable to locate package apt-get不能定位到包,有两种情况,一种是自己输入的包名字错误,确实找不到.另一种可能是执行sudo apt-get install之前更换 ...

  4. 【luogu P3063 [USACO12DEC]牛奶的路由Milk Routing】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3063#sub 我很好奇这道题为什么没被收入SPFA好题 #include <cstdio> #i ...

  5. 【luogu P2023 [AHOI2009]维护序列】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2023 把P3373改一改直接粘过来就A #include <iostream> #include ...

  6. Angularjs 数据双向绑定

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. 第一次写C语言小程序,可以初步理解学生成绩管理系统的概念

    1 成绩管理系统概述 1.1  管理信息系统的概念  管理信息系统(Management Information Systems,简称MIS),是一个不断发展的新型学科,MIS的定义随着科技的进步也在 ...

  8. oracle 完整性约束的禁用启用以及对表的影响,表的修改和复制

    primary key ----表的唯一性约束,不能为空,且不能有重复值 foreign key ----俩表之间的约束,启用之时,在删除数据时需要先删除父表数据,再删除子表数据 禁用方式为:alte ...

  9. CentOS 7设置网卡开机自动启用

    一.查看网卡配置 root权限 [root@dbsyn ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue ...

  10. cornerstone提交报错"but is missing"以及xocde提示"missing from working copy"

    问题描述 xocde提示"missing from working copy" 虽然这种警告不会影响程序到运行,但是数量很多,而且在svn提交的时候回出现这种问题 使用的svn工具 ...