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的更多相关文章

  1. 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree

    [抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...

  2. 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 ...

  3. [Lintcode]Inorder Successor in Binary Search Tree(DFS)

    题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...

  4. 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 ...

  5. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  6. [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 ...

  7. Binary Search Tree In-Order Traversal Iterative Solution

    Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...

  8. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. CIQRCodeGenerator Core Image Filter Reference

    https://developer.apple.com/library/prerelease/content/documentation/GraphicsImaging/Reference/CoreI ...

  2. 在JavaScript中,arguments是对象的一个特殊属性。

    arguments对象 function函数的内置参数的"数组"/"集合":同时arguments对象就像数组,但是它却不是数组. 常用属性: 1.length ...

  3. TemplateDataField

    .aspx <ig:TemplateDataField Key="TemplateField_0"> <Header Text="selected&qu ...

  4. Cloudservice程序设置Idle timeout

    部署的云服务程序,默认的idle timeout是4分钟,意味着如果你通过一个workerrole发布了wcf服务,客户端第一次调用服务方法后,再过4分钟尝试去重新调用服务,会报错,具体测试如下: 1 ...

  5. Only one statement is allowed per batch. A batch separator, such as 'GO', might be required between statements.

    When I added the file in VS I forgot to set Build Action = None from the file properties.

  6. rpm 命令

    这些事rpm的常用参数!!! 你可以在linux下man 一下rpm就知道了!!! 不过是英文的,不然你可以百度一下rpm就知道了额!!! 下面我帮你贴几个!!!!rpm 常用命令1.安装一个包 # ...

  7. LoadScript

    function loadScripts(urls, callback) { if (typeof (urls) === "string"){ urls = [urls]; } v ...

  8. 对AccessViolationException的一些总结

    引言 开发Winform程序时,应用程序出现了异常,整个应用程序崩溃自动退出了.在断点调试后,发现异常是AccessViolationException.所以对周围的语句加上了异常的处理机制.但是接下 ...

  9. 获取C#代码执行的时间(精确到毫秒)

    using System.Diagnostics;//引用相关的命名空间Stopwatch st=new Stopwatch ();//实例化类st. Start();//开始计时 //需要统计时间的 ...

  10. iframe和frameset的使用

    iframe是:inner frame的缩写, 必须指明src属性,不能直接在里面写内容, 否则不会显示. 可以载入html, *.图片文件, txt文件等等. html的属性中,表示长度高度的尺寸属 ...