333. 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 of its descendants.
Here's an example:

    10
    / \
   5  15
  / \   \ 
 1   8   7

The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree's size, which is 3.

一刷
題解:
如果root比左孩子要大是不夠的,還要比左孩子的subtree的最小值要大。于是考慮創(chuàng)建一個(gè)object來保存。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    class Result{
        int size;
        int lower;
        int upper;
        // size of current tree, range of current tree [rangeLower, rangeUpper]
        
        Result(int size, int lower, int upper){
            this.size = size;
            this.lower = lower;
            this.upper = upper;
        }
    }
    
    int max = 0;    
    
    public int largestBSTSubtree(TreeNode root) {
        if(root == null) return 0;
        traverse(root);
        return max;
    }
    
    private Result traverse(TreeNode root){
        if(root == null) return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE);
        Result left = traverse(root.left);
        Result right = traverse(root.right);
        if(left.size == -1 || right.size == -1 || root.val <= left.upper || root.val >= right.lower)
                return new Result(-1, 0, 0);
        int size = left.size + 1 + right.size;
        max = Math.max(size, max);
        return new Result(size, Math.min(left.lower, root.val), Math.max(right.upper, root.val));
    }
  
}

二刷
題意是給一個(gè)binary tree, 問里面滿足binary search tree的subtree的節(jié)點(diǎn)數(shù)目是多少。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    class Subtree{
        int lower;
        int upper;
        int size;
        
        Subtree(int lower, int upper, int size){
            this.lower = lower;
            this.upper = upper;
            this.size = size;
        }
    }
    int max = 0;
    public int largestBSTSubtree(TreeNode root) {
        if(root == null) return 0;
        traverse(root);
        return max;
    }
    
    private Subtree traverse(TreeNode root){
        if(root == null){
            return new Subtree(Integer.MAX_VALUE, Integer.MIN_VALUE, 0);
        }
        Subtree left = traverse(root.left);
        Subtree right = traverse(root.right);
        int size = 0;
        if(left.size!= -1 && right.size!=-1 && root.val > left.upper && root.val<right.lower) size = left.size + 1 + right.size;
        else return new Subtree(0, 0, -1);
        max = Math.max(size, max);
        return new Subtree(Math.min(left.lower, root.val), Math.max(right.upper, root.val), size);//narrow the range
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,925評(píng)論 0 33
  • 先說說我先這篇文章的目的,一,總結(jié)項(xiàng)目中遇到的問題,避免以后再犯同樣的錯(cuò)誤.二,是記錄自己的踩過得坑. 在項(xiàng)目...
    sankun閱讀 982評(píng)論 0 0
  • 回顧許多成功的企業(yè)或個(gè)人都會(huì)發(fā)現(xiàn)他們的一個(gè)共同特點(diǎn),就是在他們確定了焦點(diǎn)目標(biāo)之后,就會(huì)專注用心地做好一件...
    快樂的老露閱讀 496評(píng)論 0 0
  • 深圳的最后一晚,我總覺得我需要留下點(diǎn)什么。 散落心底的萬千情緒,不知從哪一處撿起;想要簡單記個(gè)流水賬,又于心不甘。...
    夏塔普閱讀 279評(píng)論 0 0
  • 如果客戶是異性,做客情時(shí)應(yīng)如何保持適當(dāng)?shù)木?問題: 剛聽你前幾天的答疑,發(fā)現(xiàn)如果銷售與客戶如果為異性,年齡差得又不...
    SI玲閱讀 1,352評(píng)論 0 0

友情鏈接更多精彩內(nèi)容