lintcode448- Inorder Successor in Binary Search Tree- medium

Posted jasminemzy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode448- Inorder Successor in Binary Search Tree- medium相关的知识,希望对你有一定的参考价值。

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.

Notice

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

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.

Challenge 

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

 

1. 递归法。

a. 如果root.val大了,答案要么去左边找,要么是当前root点(p是左子树的右下角最后一个点,这种情况课巧妙地用返回null来标记)。想清楚a非常重要!

b.如果root.val等于了,那要去右子树找

c.如果root.val小了,那要去右子树找(p都在右边,p的后继更在右边了)

 

2. 也递归法,处理方法更intuitive但麻烦:

要查找的点是否有右孩子:如果有,简单,直接找右子树的最小节点。如果没有,则找到比该节点大且相差最小的父节点(可用stack回溯)。

 

1. 九章实现的简洁算法,学

// version: 高频题班
public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        // write your code here
        if (root == null || p == null) {
            return null;
        }

        if (root.val <= p.val) {
            return inorderSuccessor(root.right, p);
        } else {
            TreeNode left = inorderSuccessor(root.left, p);
            return (left != null) ? left : root;
        }
    }
}

 

 

2.自己实现的stack回溯。

/**
 * 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) {
        // write your code here
        return helper(root, p, new Stack<TreeNode>());
    }
    
    public TreeNode helper(TreeNode root, TreeNode p, Stack<TreeNode> stack) {
        if (root == null) {
            return null;
        }
        if (p.val > root.val) {
            stack.push(root);
            return helper(root.right, p, stack);
        } else if (p.val < root.val) {
            stack.push(root);
            return helper(root.left, p, stack);
        } else {
            if (p.right != null) {
                TreeNode successor = p.right;
                while (successor.left != null) {
                    successor = successor.left;
                }
                return successor;
            } else {
                if (stack.isEmpty()) {
                    return null;
                }
                TreeNode successor = stack.pop();
                while (successor.val <= p.val && !stack.isEmpty()) {
                    successor = stack.pop();
                }
                // 处理当p是最后一个点的情况。
                if (successor.val <= p.val) {
                    successor = null;
                }
                return successor;
            }
        }
    }
}

 

以上是关于lintcode448- Inorder Successor in Binary Search Tree- medium的主要内容,如果未能解决你的问题,请参考以下文章

[LintCode] Inorder Successor in Binary Search Tree

lintcode-medium-Construct Binary Tree from Inorder and Postorder Traversal

lintcode-medium-Construct Binary Tree from Preorder and Inorder Traversal

73 前序遍历和中序遍历树构造二叉树

94. Binary Tree Inorder Traversal(inorder ) ***(to be continue)easy

如何基于 preorder&inorder 或 postorder&inorder 遍历构造非二叉树?