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

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 };
时间: 2024-08-01 06:43:24

[LeetCode] Inorder Successor in BST的相关文章

[LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. The successor of a node p is the node with the smallest key greater than p.val. You will have direct access to the node but not to the root of the tree.

[Lintcode] Inorder Successor in BST

Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Example Given tree = [2,1] and node = 1: 2 / 1 return node 2. Given tree = [2,1,3] and node = 2: 2 / 1 3 return node 3. Note If

[Locked] Inorder Successor in BST

Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Example Given tree = [2,1] and node = 1: 2 / 1 return node 2. Given tree = [2,1,3] and node = 2: 2 / \1 3 return node 3. Note I

[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点

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. 给一个二叉搜索树和它的一个节点,找出它的中序后继节点,如果没有返回null. 解法1: 用中序遍历二叉搜索树,当找到root.val = p.val的时

Leetcode 285: Inorder Successor in BST

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. 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 *

*Inorder Successor in BST

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. 解法一:俺自个儿的方法,居然跑了16ms...妈蛋! public class Solution { public TreeNode inorderSu

Inorder Successor in BST 解答

Question 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. Solution -- Iterative Inorder result is an ascending array for BST.

285. Inorder Successor in BST

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. 本题要求按中序遍历节点p的下一个节点是什么.这道题还是很有难度的,虽然递归实现起来比较容易.思路是,先思考,一个节点按中序的话,它的后继节点是什么呢?有

285. Inorder Successor in BST - Medium

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. Example 1: Input: root = [2,1,3], p = 1 2 / 1 3 Output: 2 Example 2: Input: