• 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;
                }
            }
        }
    }
  • 相关阅读:
    自定义dialog
    利用jquery实现自动登录
    文件的上传
    一些想法
    利用ajax实现分页效果
    自动化构建工具gulp的基础了解
    javascript模块化---requirejs
    交互ajax
    聊聊javascript的事件
    谈谈bootstrap在实践中的应用
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7681028.html
Copyright © 2020-2023  润新知