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 node in the BST.
If the given node has no in-order successor in the tree, returnnull.
分析:
给一个二叉查找树,以及一个节点,求该节点的中序遍历后继,如果没有返回 null。
一棵BST定义为:
节点的左子树中的值要严格小于该节点的值。
节点的右子树中的值要严格大于该节点的值。
左右子树也必须是二叉查找树。
一个节点的树也是二叉查找树。
这道题的中心思想就是先把root结点移动到p旁边去,因为binary search tree的特点是 左子树小于 根,根小于右子树。
while(root不为空,并且root和p不相等)
如果判断了root .val > p.val,就说明在根的左侧,所以一点一点往左移动,直到root 为空,或者root等于p.
如果根小于p,说明在右侧,要右移.
关于返回值
1.如果P没有右子节点,而且P是他父节点的左孩子,则要找的后继节点就是他的父节点
2. 如果P没有右子节点,而且P是他父节点的右孩子,则要找的后继节点就是他祖先节点里第一个是他父节点左子树的节点的父节点
3. 如果P有右子节点,则要找的后继节点就是以它右孩子为根的子树的最左面那个节点.
1 2对应的是程序里 return successor那部分
3 对应的是最下面那部分
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode successor = null;
while (root != null && root != p) {
if (root.val > p.val) {
successor = root;
root = root .left;
} else {
root = root.right;
}
} if (root == null) {
return null;
} if (root.right == null) {
return successor;
} root = root.right;
if (root.left != null) {
root = root.left;
}
return root;
}
}
定义后继点,
根非空,根不等p进循环
根大p则 后继为根 根左移。
小等则左移,
退循环
根空返空,
根右空,返后继,
根右移,
root向左走到头,
返回根
Inorder Successor in Binary Search Tree的更多相关文章
- 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree
[抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...
- 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 ...
- [Lintcode]Inorder Successor in Binary Search Tree(DFS)
题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...
- 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
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Binary Search Tree In-Order Traversal Iterative Solution
Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- 二维码(QRcode)容量的计算与版本
4.版本信息:即二维码的规格,QR码符号共有40种规格的矩阵(一般为黑白色),从21x21(版本1),到177x177(版本40),每一版本符号比前一版本每边增加4个模块. 177 = 21+(40- ...
- [Java]double初始化问题
如下: 1. 直接初始化 double[][] embossFilter = {{-1/9, 0, 1/9}, {-1/9, 1/9, 1/9}, {-1/9, 0, 1/9}}; 2. 赋值初始化 ...
- MySQL的外键是什么和它的作用
从上图可以看见,表1添加一个外键,这个外键就是表2中的学号字段,那么这样表1就是主表,表2就是子表.所以结合2张表就能保持数据的一致性.完整性. 外键的一些事项:1.表1可以有一个或者多个外键,也可以 ...
- linux 查看内存的插槽数
[root@web ~]# dmidecode|grep -P -A5 "Memory\s+Device"| grep Size | grep -v Range #linux查看内 ...
- ubuntu亮度调节失效
ctrl+alt+T 打开终端 输入下面的指令 sudo touch /usr/share/X11/xorg.conf.d/20-intel.conf 2 再输入下面的指令: sudo gedit / ...
- MySQL学习笔记——约束
1.约束是在表上强制执行的数据检验规则,约束主要用于保证数据库的完整性. 2.当表中数据有相互依赖性时,可以保护相关的数据不被删除. 3.大部分数据库支持下面五类完整性约束: - NOT NULL非空 ...
- 创建menu文件
一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...
- C#如何把List of Object转换成List of T具体类型
上周码程序的时候碰到个问题,因为设计上的约束,一个方法接受的参数只能为List<object>类型,然而该方法需要处理的真实数据则是确定的List<Currency>.然而C# ...
- python抓取网站URL小工具
1.安装Python requests模块(通过pip): 环境搭建好了! 2.测试一下抓取URL的过程: 抓取出来的URL有JavaScript代码,正则上还有待更加完善,有兴趣的可以研究下~! 工 ...
- jquery 判断网络图片,或网络文件是否存在
$.ajax({ url : picSrc, async : false, type : 'HEAD', error : function() { picSrc = "https://ss0 ...