国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

333. Largest BST Subtree

DataPipeline / 2042人閱讀

     10
    / 
   5  15
  /     
 1   8   7

return 3
public class Solution {
    public int largestBSTSubtree(TreeNode root) {
        if(root == null) return 0;
        int[] res = recursive(root);
        return res[2];
    }
    
    public int[] recursive(TreeNode root){
        int[] res = new int[5];
        // res[0] BST or Not?
        // res[1] total number nodes of subtree
        // res[2] max number of BST subtree 
        // res[3] min
        // res[4] max
        res[0] = 1; res[3] = Integer.MAX_VALUE; res[4] = Integer.MIN_VALUE;
        if(root == null) return res;
        
        int[] resL = recursive(root.left);
        int[] resR = recursive(root.right);
        if(resL[0] == 0 || resR[0] == 0 || resL[4] >= root.val || resR[3] <= root.val) 
            res[0] = 0;
        res[1] = resL[1] + resR[1] + 1;
        res[2] = (res[0] == 1) ? res[1]: Math.max(resL[2], resR[2]);
        res[3] = root.val < resL[3] ? root.val : resL[3];
        res[4] = root.val > resR[4] ? root.val : resR[4];
        return res;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/70080.html

相關(guān)文章

  • LeetCode[333] Largest BST Subtree

    摘要:復(fù)雜度思路考慮對(duì)于每一個(gè)節(jié)點(diǎn)來說,能組成的的。那么并且所以我們需要兩個(gè)返回值,一個(gè)是這個(gè)是不是,另一個(gè)是當(dāng)前的能組成的最大的值。代碼這個(gè)能構(gòu)成一個(gè)這個(gè)不能構(gòu)成一個(gè) LeetCode[333] Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary SearchTree (B...

    WrBug 評(píng)論0 收藏0
  • [LeetCode] 333. Largest BST Subtree

    Problem Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its descen...

    tigerZH 評(píng)論0 收藏0
  • [LeetCode] Largest BST Subtree

    Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note: A subtree must include all ...

    Youngdze 評(píng)論0 收藏0
  • [LeetCode] 776. Split BST

    Problem Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, whil...

    baiy 評(píng)論0 收藏0
  • 二叉排序樹

    摘要:節(jié)點(diǎn)的構(gòu)造函數(shù)默認(rèn)為其初始化都是。二叉排序樹插入插入節(jié)點(diǎn)只要遵循一個(gè)原則就好大與就向中插入,小于就向插入。初始化數(shù)據(jù)樹現(xiàn)在來試試實(shí)例化一個(gè)來看看效果。 JavaScript 數(shù)據(jù)結(jié)構(gòu)篇 之 BST 前言 有段時(shí)間沒有寫文章了,整個(gè)人感覺變得有點(diǎn)懶散了,大家可不要向我一樣哦~今天開始 seaconch 打算開始記錄 JavaScript 數(shù)據(jù)結(jié)構(gòu)的學(xué)習(xí)經(jīng)歷。至此,開始。 課本:《學(xué)習(xí)J...

    Soarkey 評(píng)論0 收藏0

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

0條評(píng)論

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