zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }
  • 相关阅读:
    2016701010126 2016-2017-2《java程序设计》集合
    201671010126 2016-2017-2《Java程序设计》第六周
    201671010126 2016-2017-2《Java程序设计》总结
    201671010128 2017-12-17《Java程序设计》之并发
    201671010128 2017-11-10《Java程序设计》之应用程序部署(2)
    201671010128 2017-11-29《Java程序设计》之应用程序部署
    201671010128 2017-11-29《Java程序设计》之Swing用户界面组件
    201671010128 2017-11-19《Java程序设计》之事件处理技术
    201671010128 2017-11-12《Java程序设计》之图形程序设计
    201671010128 2017-11-05《Java程序设计》之集合
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7681028.html
Copyright © 2011-2022 走看看