`

Inorder Successor in BST

    博客分类:
  • Tree
 
阅读更多
[分析]
参考https://leetcode.com/discuss/59728/10-and-4-lines-o-h-java-c
& https://leetcode.com/discuss/59787/share-my-java-recursive-solution
不理解参考代码中为什么最后需要额外判断left.val > p.val

public class Solution {
    // Method 3: recursion
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        while (root != null && root.val <= p.val)
            root = root.right;
        if (root == null) return null;
        TreeNode left = inorderSuccessor(root.left, p);
        return left != null ? left : root;
    }
    public TreeNode inorderPrecessor(TreeNode root, TreeNode p) {
        while (root != null && root.val >= p.val)
            root = root.left;
        if (root == null) return null;
        TreeNode right = inorderPrecessor(root.right, p);
        return right != null ? right : root;
    }
    
    // Method 2: optimize findSuccessor
    private TreeNode findSuccessor(TreeNode root, TreeNode p) {
        TreeNode cand = null; // smallest node which is larger than p, or the last left-turn node
        while (root != null) {
            if (root.val <= p.val) {
                root = root.right;
            } else {
                cand = root;
                root = root.left;
            }
        }
        return cand;
    }
    
    // Method 1: self
    public TreeNode inorderSuccessor1(TreeNode root, TreeNode p) {
        if (root == null || p == null) return null;
        if (p.right != null)
            return findMin(p.right);
        else
            return findSuccessor(root, p);
    }
    private TreeNode findMin(TreeNode root){
        if (root == null) return root;
        while (root.left != null)
            root = root.left;
        return root;
    }
    private TreeNode findSuccessor1(TreeNode root, TreeNode p) {
        LinkedList<TreeNode> path = new LinkedList<TreeNode>();
        TreeNode curr = root;
        while (curr != p) {
            path.push(curr);
            if (p.val > curr.val) {
                curr = curr.right;
            } else {
                curr = curr.left;
            }
        }
        while (!path.isEmpty()) {
            TreeNode cand = path.pop();
            if (cand.val > p.val) return cand;
        }
        return null;
    }
}
分享到:
评论

相关推荐

    java-leetcode题解之Inorder Successor in BST.java

    在Java语言中解决LeetCode上的中序后继在二叉搜索树中的问题,首先需要理解二叉搜索树(BST)的特性以及中序遍历的概念。二叉搜索树是一种特殊的二叉树,其中每个节点都遵循这样的规则:它的左子树中所有节点的值都...

    leetcode285-InorderSuccessorInBST-Leetcode-285::red_question_mark:一种使用迭代中序遍历来寻找BST中节点的中序后

    我们将首先解释二叉搜索树(BST)的基本概念,然后详细解析如何通过迭代中序遍历来找到给定节点的中序后继。 二叉搜索树是一种特殊的二叉树,每个节点的值都大于其左子树中任何节点的值,同时小于其右子树中任何...

    leetcodetreenode-inorder-successor-in-bst:BST中的有序后继

    leetcode 树节点BST 中的有序后继 给定一个二叉搜索树和其中的一个节点,在 BST 中找到该节点的有序后继。...helper(root,inorder); for ( TreeNode node : inorder){ if (node . val &gt; p . val) return

    leetcode285-InorderSuccessorBST:LeetCode285InorderSuccessorinBSTinJava

    Inorder Successor in BST in Java 如果对此问题感兴趣,请访问以下 LeetCode URI: 如果我的方法描述中包含整数,请访问我博客中的以下帖子: 继续阅读和实验; 这是最好的学习方式。 享受; 约翰

    leetcode110-JDK:硬啃JDK

    譬如TreeMap中就有一些我遇到的关于二叉树的题目LeetCode173binarysearchtreeiteratorLIntCode448InorderSuccessorinBST(LeetCode收费=.=)LeetCode110.BalancedBinaryTree等等集合阅读套路:先从最简单的CRUD开始...

    数据结构课程设计二叉排序树的实现

    // Node with two children: Get the inorder successor (smallest in the right subtree) TreeNode* temp = FindMin(root-&gt;right); // Copy the inorder successor's content to this node root-&gt;data = temp-...

    寻找中序线索化二叉树指定结点的前驱.zip

    在二叉树的每个节点上,我们可以设置两个附加指针:一个称为“中序前驱”(in-order predecessor)指针,另一个称为“中序后继”(in-order successor)指针。 在中序线索化二叉树中,对于任何节点: - 如果节点是...

    leetcode285-CodingPractice:编码实践

    7. **285 - 树的 inorder successor in O(log n)**:这是上述230题的一个优化版本,要求在O(log n)的时间复杂度内找到中序后继。 8. **98 - 验证二叉搜索树**:判断给定的二叉树是否为BST,需要检查每个节点的值...

    HDT-7:工作表编号7

    // delete the inorder successor node.right = deleteRec(node.right, node.key); } return node; } int minValue(Node node) { int minV = node.key; while (node.left != null) { minV = node.left.key...

    《算法导论》完整的课件下载 方便自学之用 从最基础的开始讲解

    中序遍历(Inorder Traversal)是访问二叉搜索树节点的一种方法,按照递增顺序打印节点的键值。其步骤如下: 1. 如果节点不为空,则对左子树进行中序遍历。 2. 打印当前节点的键值。 3. 对右子树进行中序遍历。 ...

Global site tag (gtag.js) - Google Analytics