LintCode 448 Inorder Successor in BST (BST后继)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tc_To_Top/article/details/89578613

Description

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, return null.

It's guaranteed p is one node in the given tree. (You can directly compare the memory address to find p)

Have you met this question in a real interview?  Yes

Problem Correction

Example

Example 1:

Input: tree = {1,#,2}, node value = 1
Output: 2
Explanation:
  1
   \
    2

Example 2:

Input: tree = {2,1,3}, node value = 1
Output: 2
Explanation: 
    2
   / \
  1   3

Binary Tree Representation

Challenge

O(h), where h is the height of the BST.

题目链接:https://www.lintcode.com/problem/inorder-successor-in-bst/description

题目分析:感觉是面试常考题,比较直白的做法就是分情况讨论,有右子树,那么必然是右子树最小值,没有右子树就看父亲了,是父亲的左孩子那答案就是父亲,是父亲的右孩子,那从低向上知道找到某个点是父亲的左孩子,则此时的父亲就是答案

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */


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 findMin(TreeNode p) {
        while (p.left != null) {
            p = p.left;
        }
        return p;
    }
    
    public TreeNode findFa(TreeNode root, TreeNode p, HashMap<TreeNode, TreeNode> faMap) {
        if (root == p) {
            return null;
        }
        TreeNode fa = null;
        while (root != null) {
            fa = root;
            if (root.val > p.val) {
                root = root.left;
            } else if (root.val < p.val) {
                root = root.right;
            } else {
                break;
            }
            faMap.put(root, fa);
        }
        return faMap.get(p);
    }
     
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (root == null) {
            return null;
        }
        if (p.right != null) {
            return findMin(p.right);
        }
        HashMap<TreeNode, TreeNode> faMap = new HashMap<>();
        TreeNode fa = findFa(root, p, faMap);
        if (fa == null ){
            return null;
        }
        if (fa.left == p) {
            return fa;
        } else {
            while (faMap.get(fa) != null) {
                if (fa == faMap.get(fa).left) {
                    return faMap.get(fa);
                }
            }
        }
        return null;
    }
}

由于题目给了根,那么只要root.val比p.val大,就把root作为p的后继,再将root二分不断逼近p,类似有序数组的upper_bound

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */


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) {
        TreeNode successor = null;
        while (root != null) {
            if (root.val > p.val) {
                successor = root;
                root = root.left;
            } else {
                root = root.right;   
            }
        }
        return successor;
    }
}

猜你喜欢

转载自blog.csdn.net/Tc_To_Top/article/details/89578613
BST