二叉树查找树中序后继 · Inorder Successor in Binary Search Tree
[抄题]:
给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null
[思维问题]:
不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的。
[一句话思路]:
比root大时直接扔右边递归,比root小时 考虑是左边递归还是就是root
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 要定义left节点,留着做后续的比较
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
就是树。
递归表达式,直接出结果。
[其他解法]:
太麻烦了
[Follow Up]:
[LC给出的题目变变变]:
锁着了
public class Solution {
/*
* @param root: The root of the BST.
* @param p: You need find the successor node of p.
* @return: Successor of p.
*/
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if (root == null || p == null) {
return null;
}
if (p.val >= root.val) {
return inorderSuccessor(root.right, p);
}
else {
TreeNode left = inorderSuccessor(root.left, p);
return (left != null) ? left : root;
}
}
}
二叉树查找树中序后继 · Inorder Successor in Binary Search Tree的更多相关文章
- Inorder Successor in Binary Search Tree
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...
- [Lintcode]Inorder Successor in Binary Search Tree(DFS)
题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...
- Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree
struct node { int val; node *left; node *right; node *parent; node() : val(), left(NULL), right(NULL ...
- [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [Swift]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that ...
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...
随机推荐
- Eclipse修改workspace目录的几种方式
Eclipse是一款很强的Java IDE,我们在开始的时候,往往设定了默认的workspace,当用久在之后,我们可能要去更改一下workspace的位置.下面有几种方法可以更改workspace的 ...
- mdm9x07 ATC AT+QCFG usbnet
1 中文AT命令详解 1.1. AT+QCFG 扩展配置 AT+ QCFG 扩展配置 测试命令 AT+QCFG=? 响应 …… +QCFG: "usbnet" ...
- Frequently Asked Questions
转自:http://www.tornadoweb.org/en/stable/faq.html Frequently Asked Questions Why isn’t this example wi ...
- Hibernate 零散知识点
1 get方法和load方法查询时的区别: 如果在缓存中没有找到相应的对象,get会直接访问数据库并返回一个完全初始化的对象,过程中可能涉及多次数据库调用:而load会返回一个代理对象,只有在对象ge ...
- 笔记本电脑安装centos 7(转)
1. 下载 CentOS 镜像 下载地址 : https://wiki.centos.org/Download, 我下载的是1406 2. 使用 UltraISo 将镜像刻录到U盘 UltraISo ...
- C# 中 PadLeft和PadRight 的用法
C# 中 PadLeft和PadRight 的用法 在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char pa ...
- TensorFlow利用A3C算法训练智能体玩CartPole游戏
本教程讲解如何使用深度强化学习训练一个可以在 CartPole 游戏中获胜的模型.研究人员使用 tf.keras.OpenAI 训练了一个使用「异步优势动作评价」(Asynchronous Advan ...
- FIO read测试结果偏离
工作中发现一个fio问题,测试组测试出来的数据read速度一个是17.0G/s,一个是13.2G/s.要知道我后台只有24块7.2k RPM的机械硬盘啊!怎么也不可能有这样的速度. 回家之后我模拟了实 ...
- Docker-compose 在up之后,各个子服务容器的hosts文件中不能找到父服务的域名
使用命令查看docker当前建立的网络: docker network ls 发现docker-compose up确实建立了网络xx_default 使用命令查看该网络详细信息: docker in ...
- python函数进阶
知识内容: 1.函数即变量 2.嵌套函数 3.lambda表达式与匿名函数 4.递归函数 5.函数式编程简介 6.高阶函数与闭包 一.函数即变量 1.变量的本质 声明一个变量,在python里本质上讲 ...