[LeetCode] Inorder Successor in BST

简介: Problem Description: Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Problem Description:

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Note: If the given node has no in-order successor in the tree, return null.


There are just two cases:

  1. The easier one: p has right subtree, then its successor is just the leftmost child of its right subtree;
  2. The harder one: p has no right subtree, then a traversal is needed to find its successor.

Traversal: we start from the root, each time we see a node with val larger than p -> val, we know this node may be p's successor. So we record it in suc. Then we try to move to the next level of the tree: if p -> val > root -> val, which means p is in the right subtree, then its successor is also in the right subtree, so we update root = root -> right; if p -> val < root -> val, we update root = root -> left similarly; once we find p -> val == root -> val, we know we've reached at p and the current sucis just its successor.

The code is as follows. You may try some examples to see how it works :-)

 1 class Solution {
 2 public:
 3     TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
 4         if (p -> right) return leftMost(p -> right);
 5         TreeNode* suc = NULL;
 6         while (root) {
 7             if (p -> val < root -> val) {
 8                 suc = root;
 9                 root = root -> left;
10             }
11             else if (p -> val > root -> val)
12                 root = root -> right; 
13             else break;
14         }
15         return suc;
16     }
17 private:
18     TreeNode* leftMost(TreeNode* node) {
19         while (node -> left) node = node -> left;
20         return node;
21     }
22 };

 

目录
相关文章
Leetcode 230. Kth Smallest Element in a BST
先来看下二叉搜索树的性质,对于任意一个非叶子节点,它的左子树中所有的值必定小于这个节点的val,右子树中所有的值必定大于或等于当前节点的val。 这条性质就保证了如果我们对二叉搜索树做中序遍历,中序遍历的结果肯定是有序的。对于此题而言,我们只需要拿到中序遍历结果,然后返回第k个即可,时间复杂度是O(n)。
63 1
LeetCode 109. Convert Sorted List to BST
给定一个链表,其中元素按升序排序,将其转换为高度平衡的BST。 对于这个问题,高度平衡的二叉树被定义为:二叉树中每个节点的两个子树的深度从不相差超过1。
58 0
LeetCode 109. Convert Sorted List to BST
LeetCode 108. Convert Sorted Array to BST
给定一个数组,其中元素按升序排序,将其转换为高度平衡的BST。 对于这个问题,高度平衡的二叉树被定义为:二叉树中每个节点的两个子树的深度从不相差超过1。
76 0
LeetCode 108. Convert Sorted Array to BST
【LeetCode538】把二叉搜索树转换为累加树(BST中序)
一定注意有BST的条件,BST的特性是中序遍历(左中右)得到从小到大的序列,而题目求的是大于等于当前结点的值替换原值——注意这里是大于等于!!所以就不是单纯的中序,而是逆中序(右
106 0
【LeetCode538】把二叉搜索树转换为累加树(BST中序)
|
算法
​LeetCode刷题实战333:最大 BST 子树
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
152 0
[leetcode/lintcode 题解] 算法面试真题:BST的中序前驱节点
[leetcode/lintcode 题解] 算法面试真题:BST的中序前驱节点
[leetcode/lintcode 题解] 算法面试真题:BST的中序前驱节点
|
算法 Java Python
LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历。 Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出:...
967 0
|
Java
[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/难度:Medium题目:105.
975 0
[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/难度:Medium题目:106.
1134 0