成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

LeetCode[285] Inorder Successor in BST

oneasp / 759人閱讀

摘要:復(fù)雜度思路找的是比這個大的最小值。代碼如果是小于等于的值,就要往右移動比大的值都可能是,所以要保留

LeetCode[285] Inorder Successor in BST

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Note: If the given node has no in-order successor in the tree, return null.

Iteration

復(fù)雜度
O(lgN), O(lgN)

思路
找的是比這個node大的最小值。

代碼

public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
    if(root == null) return null;
    TreeNode res = null;
    while(root != null) {
        // 如果是小于等于的值,就要往右移動
        if(root.val <= p.val) {
            root = root.right;
        }
        // 比root大的值都可能是successor,所以要保留
        else {
            res = root;
            root = root.left;
        }
    }
    return res;
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65252.html

相關(guān)文章

  • [LeetCode] 285. Inorder Successor in BST

    Problem Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. Example 1: Input: ...

    focusj 評論0 收藏0
  • LeetCode 二叉樹專項】二叉搜索樹中的中序后繼(285

    摘要:解法二迭代中序遍歷分析作者二叉搜索樹的中序后繼是中序遍歷中當(dāng)前節(jié)點(diǎn)之后最小的節(jié)點(diǎn)。 文章目錄 1. 題目1.1 示例1.2 說明1.3 限制 2....

    ccj659 評論0 收藏0
  • [Leetcode] Inorder Successor in BST 二叉搜索樹中序下一個

    摘要:所以我們要找的上面,實際上是從目標(biāo)節(jié)點(diǎn)向根節(jié)點(diǎn)回溯時,第一個比目標(biāo)節(jié)點(diǎn)大的節(jié)點(diǎn)。 Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has n...

    marek 評論0 收藏0
  • [Leetcode] Binary Search Tree Iterator 二叉搜素樹迭代器

    摘要:代碼先找到第一個節(jié)點(diǎn),并把路徑入棧棧為空時不再有下一個棧頂是待返回元素如果該元素有右節(jié)點(diǎn),把它的右節(jié)點(diǎn)及右節(jié)點(diǎn)的所有左邊節(jié)點(diǎn)都壓入棧中 Binary Search Tree Iterator 最新更新:https://yanjia.me/zh/2019/02/... Implement an iterator over a binary search tree (BST). Your...

    shixinzhang 評論0 收藏0
  • LeetCode 272 Closest Binary Tree Traversal II 解題思路

    摘要:原題網(wǎng)址題意在二叉搜索樹當(dāng)中找到離最近的個數(shù)。解題思路由于二叉搜索數(shù)的中序遍歷是有序的,比如例子中的樹,中序遍歷為。 原題網(wǎng)址:https://leetcode.com/problems... Given a non-empty binary search tree and a target value, find?k?values in the BST that are closes...

    Youngdze 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<