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

資訊專欄INFORMATION COLUMN

[LeetCode] 671. Second Minimum Node In a Binary Tr

xingpingz / 550人閱讀

Problem

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node"s value is the smaller value among its two sub-nodes.

Given such a binary tree, you need to output the second minimum value in the set made of all the nodes" value in the whole tree.

If no such second minimum value exists, output -1 instead.

Example 1:

Input: 
    2
   / 
  2   5
     / 
    5   7

Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

Input: 
    2
   / 
  2   2

Output: -1
Explanation: The smallest value is 2, but there isn"t any second smallest value.

Solution
class Solution {
    public int findSecondMinimumValue(TreeNode root) {
        if(root == null || root.left == null) return -1;
        
        int leftRes, rightRes;
        if (root.left.val == root.val) {
            leftRes = findSecondMinimumValue(root.left);
        } else {
            leftRes = root.left.val;
        }
        if (root.right.val == root.val) {
            rightRes = findSecondMinimumValue(root.right);
        } else {
            rightRes = root.right.val;
        }
        
        if (leftRes == -1 && rightRes == -1) return -1;
        else if (leftRes == -1) return rightRes;
        else if (rightRes == -1) return leftRes;
        else return Math.min(leftRes, rightRes);

    }
}

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

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

相關(guān)文章

  • [Leetcode-Tree]Maximum / Minimum Depth of Binary T

    摘要:解題思路用遞歸實(shí)現(xiàn)很簡(jiǎn)單,對(duì)于每個(gè)根節(jié)點(diǎn),最大深度就等于左子樹的最大深度和右子樹的最大深度的較大值。解題思路本題的注意點(diǎn)在于如果某個(gè)根節(jié)點(diǎn)有一邊的子樹為空,那么它的深度就等于另一邊不為空的子樹的深度,其他的邏輯與上一題相同。 Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth. The maxi...

    Thanatos 評(píng)論0 收藏0
  • [Leetcode] Maximum and Minimum Depth of Binary Tre

    摘要:遞歸法復(fù)雜度時(shí)間空間遞歸??臻g思路簡(jiǎn)單的遞歸。該遞歸的實(shí)質(zhì)是深度優(yōu)先搜索。這里我們還有改進(jìn)的余地,就是用迭代加深的有限深度優(yōu)先搜索。 Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l...

    boredream 評(píng)論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡(jiǎn)單難度攻略已經(jīng)到題,所以后面會(huì)調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...

    tain335 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語(yǔ)言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • [LintCode/LeetCode] Construct Binary Tree from Tr

    摘要:做了幾道二分法的題目練手,發(fā)現(xiàn)這道題已經(jīng)淡忘了,記錄一下。這道題目的要點(diǎn)在于找的區(qū)間。邊界條件需要注意若或數(shù)組為空,返回空當(dāng)前進(jìn)到超出末位,或超過,返回空每次創(chuàng)建完根節(jié)點(diǎn)之后,要將加,才能進(jìn)行遞歸。 Construct Binary Tree from Inorder and Preorder Traversal Problem Given preorder and inorder t...

    馬忠志 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<